标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27989 Accepted Submission(s):
6952
# include <iostream> # include <fstream> # include <cstring> # include <queue> using namespace std; int map[1001][1001]; int is[1001][1001]; int n, m; int q; struct Dian { int x, y, dir, jishu; }k1, k2; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; void f(); bool border(int, int); int main() { //fstream cin("aaa.txt"); memset(is, 0, sizeof(is)); while(cin >> n >> m) { if(n == 0 && m == 0) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> map[i][j]; cin >> q; for(int i = 0; i < q; i++) { cin >> k1.x >> k1.y >> k2.x >> k2.y; if(map[k1.x][k1.y] != map[k2.x][k2.y]) { cout << "NO" << endl; continue; } if(map[k1.x][k1.y] == 0 || map[k2.x][k2.y] == 0) { cout << "NO" << endl; continue; } f(); } } return 0; } void f() { memset(is, 0, sizeof(is)); queue <Dian> a; Dian tep, t; tep.x = k1.x; tep.y = k1.y; tep.dir = -1; tep.jishu = 0; a.push(tep); while(!a.empty()) { tep = a.front(); a.pop(); is[tep.x][tep.y] = 1; if(tep.x == k2.x && tep.y == k2.y) { cout << "YES" << endl; return ; } // cout << "haha" << endl; for(int i = 0; i < 4; i++) { t.x = tep.x + dir[i][0]; t.y = tep.y + dir[i][1]; t.dir = i; t.jishu = tep.jishu; if(!border(t.x, t.y)) // 出边界 continue; if(is[t.x][t.y]) continue; // cout << t.x << " " << t.y <<endl; //cout << is[t.x][t.y] << endl; if(map[t.x][t.y] && !(t.x == k2.x && t.y == k2.y)) //是数字并且 continue; if(tep.dir != t.dir && tep.dir != -1)// 走的方向判断 t.jishu++; // cout << t.jishu << endl; if(t.jishu > 2) continue; a.push(t); is[t.x][t.y] = 1; } } cout << "NO" << endl; } bool border(int x1, int y1) { if(x1 > 0 && x1 <= n && y1 > 0 && y1 <= m) return true; return false; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5399299.html