| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
G++ 用pair过不了为啥以下代码是用C++过的,用G++就是过不去
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
int vis[10][10];
int dir[8][2] = {{2,1},{2,-1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,-2},{-1,2}};
int sx, sy, ex, ey;
int BFS() {
pair<int, int> st = make_pair(sx, sy);
queue< pair<int, int> > Q;
Q.push(st);
vis[sx][sy] = 1;
if(sx == ex && sy == ey) return 0;
int x, y, t;
int step = 0;
while(!Q.empty()) {
t = Q.size(); step++;
while(t--) {
st = Q.front(); Q.pop();
for(int i = 0; i < 8; i++) {
x = st.first + dir[i][0];
y = st.second + dir[i][1];
if(x>=0&&x<8&&y>=0&&y<8&&!vis[x][y]) {
vis[x][y] = 1;
if(x == ex && y == ey) {
return step;
}
Q.push(make_pair(x, y));
}
}
}
}
return 0;
}
int main()
{
char str1[5], str2[5];
while(scanf("%s%s", str1, str2) != EOF) {
memset(vis, 0, sizeof(vis));
sx = str1[0] - 'a'; sy = str1[1] - '0' - 1;
ex = str2[0] - 'a'; ey = str2[1] - '0' - 1;
//cout << sx << sy << " " << ex << ey << endl;
int step = BFS();
printf("To get from %s to %s takes %d knight moves.\n", str1, str2, step);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator