hdu2102广搜
还是比较简单的,广搜就行,不过我一开始以为用深搜也可以,还打了一部分代码,后来才想到是会超时的,因为题目中没有给出T的范围。
/* * hdu2102/win.cpp * Created on: 2012-11-28 * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <vector> #include <deque> #include <list> #include <functional> #include <numeric> #include <cctype> using namespace std; const int MAXN = 15; const int move[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } }; char graph[2][MAXN][MAXN]; bool visited[2][MAXN][MAXN]; int M, N; typedef struct MyPoint { int k, x, y, step; MyPoint() {k = x = y = step = 0;} MyPoint(int kk, int xx, int yy, int ss) { k = kk, x = xx, y = yy, step = ss; } }MyPoint; bool bfs(int step) { memset(visited, false, sizeof(visited)); queue<MyPoint> Q; Q.push(MyPoint(0, 1, 1, step)); visited[0][1][1] = true; int x, y, k; while(!Q.empty()) { MyPoint cur = Q.front(); Q.pop(); if(cur.step <= 0) { continue; } for(int m = 0; m < 4; m++) { k = cur.k; x = cur.x + move[m][0]; y = cur.y + move[m][1]; if(graph[k][x][y] == 'P') { return true; }else if(graph[k][x][y] == '.' && !visited[k][x][y]) { Q.push(MyPoint(k, x, y, cur.step - 1)); visited[k][x][y] = true; }else if(graph[k][x][y] == '#') { k = (int)(!k); if(graph[k][x][y] == 'P') { return true; } if(graph[k][x][y] == '.' && !visited[k][x][y]) { Q.push(MyPoint(k, x, y, cur.step - 1)); visited[k][x][y] = true; } } } } return false; } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int T, step; scanf("%d", &T); while(T--) { scanf("%d%d%d", &N, &M, &step); if(step > N * M * 2) { step = N * M * 2 + 1; } memset(graph, '*', sizeof(graph)); for(int k = 0; k < 2; k++) { getchar(); for(int i = 1; i <= N; i++) { for(int j = 1; j <= M; j++) { graph[k][i][j] = getchar(); } getchar(); } } graph[0][1][1] = '.'; if(bfs(step)) { puts("YES"); }else { puts("NO"); } } return 0; }