标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15463 Accepted Submission(s): 3859
其实很简单的: 有两个地图, 如果遇到#就到第二个地图去搜, 再次遇到#就再回到第一个地图, node里面多一个flag标记是在第几个地图 .
开始没A是因为题面坑:
1没说清是必须在T时刻遇到公主还是<=T都可以
2没说还有两个图同时都是#的情况,因为从mat1跳到mat2是不消耗时间的,所以就搞不清到底是怎样?直接GG?还是只跳一次?
#include<queue> #include<math.h> #include<algorithm> #include<string> #include<string.h> #include<stdio.h> #include<iostream> using namespace std; #define N 112345678 #define M 155 #define INF 0x3f3f3f3f struct node { int x,y,time; bool flag; }p,st; int n, m, a, b, x, y, t, cnt, ans; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; char mat1[M][M],mat2[M][M]; bool v1[M][M],v2[M][M]; void Bfs(int x, int y , int time) { ans = -1; memset(v1,0,sizeof(v1)); memset(v2,0,sizeof(v2)); queue<node>q; while(!q.empty()) q.pop(); st.x = x, st.y = y, st.time = time; st.flag = true; q.push(st); v1[st.x][st.y] = true; while(!q.empty()) { st = q.front(); //printf("front = %d %d time = %d, flag = %d\n",st.x, st.y, st.time, st.flag); q.pop(); for(int i = 0; i < 4; i++) { node next = st; next.x += dx[i], next.y += dy[i], next.time++; if(next.flag == true) { if(mat1[next.x][next.y] == ‘P‘) { ans = next.time ; return; } if(next.x < 0 || next.x >= n || next.y < 0 || next.y >=m)continue; if(mat1[next.x][next.y] == ‘*‘ || mat1[next.x][next.y] == ‘#‘&&mat2[next.x][next.y] == ‘*‘ || v1[next.x][next.y] == true || mat1[next.x][next.y] == ‘#‘&&mat2[next.x][next.y] == ‘#‘) continue; if(mat1[next.x][next.y] == ‘#‘ ) { next.flag = !next.flag; } v1[next.x][next.y] = true; } if(next.flag == false) { if(mat2[next.x][next.y] == ‘P‘) { ans = next.time ; return; } if(next.x < 0 || next.x >= n || next.y < 0 || next.y >=m)continue; if(mat2[next.x][next.y] == ‘*‘ || mat2[next.x][next.y] == ‘#‘&&mat1[next.x][next.y] == ‘*‘ || v2[next.x][next.y] == true || mat1[next.x][next.y] == ‘#‘&&mat2[next.x][next.y] == ‘#‘) continue; if(mat2[next.x][next.y] == ‘#‘ ) { next.flag = !next.flag; } v2[next.x][next.y] = true; } q.push(next); } } } int main() { int T;cin>>T; while(T--) { cin>>n>>m>>t; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) scanf(" %c", &mat1[i][j]); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) scanf(" %c", &mat2[i][j]); Bfs(0,0,0); if(ans == -1 || ans > t) cout<<"NO"<<endl; else cout<<"YES"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/wmxl/p/5358095.html