标签:des style blog http io ar color os sp
Description
Input
Output
Sample Input
Sample Output
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<set> using namespace std; const int SIZE = 110; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char maze[SIZE][SIZE]; int v[SIZE][SIZE]; int ex, ey, bx, by; int m, n, limit; struct node { int x, y; int step; }; queue<node> Q; int check(int x, int y) { if(x >= 1 && y >= 1 && x <= m && y <= n && maze[x][y] != ‘*‘) return 1; return 0; } void init() { memset(v, 0, sizeof(v)); memset(maze, 0, sizeof(maze)); while(!Q.empty()) Q.pop(); } void bfs(int bx, int by) { if(bx == ex && by == ey) { printf("yes\n"); return ; } v[bx][by] = 1; node in, out; in.x = bx; in.y = by; in.step = -1; Q.push(in); while(!Q.empty()) { out = Q.front(); Q.pop(); int x = out.x; int y = out.y; for(int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; while(check(xx, yy)) { if(!v[xx][yy]) { in.x = xx; in.y = yy; in.step = out.step+1; Q.push(in); v[xx][yy] = 1; if(xx == ex && yy == ey && in.step <= limit) //注意,判断条件是在这里。 { printf("yes\n"); return ; } } xx += dx[i]; yy += dy[i]; } } } printf("no\n"); return; } int main() { int T; scanf("%d", &T); while(T--) { init(); scanf("%d%d", &m, &n); for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) //从1开始的,不能用字符串输入。 { scanf(" %c", &maze[i][j]); } } scanf("%d%d%d%d%d", &limit, &by, &bx, &ey, &ex); bfs(bx, by); } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/a972290869/p/4108984.html