HDU 2101 A计划

来源:互联网 发布:weka算法介绍 编辑:程序博客网 时间:2024/06/10 03:32

点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=2102

AC代码       用到三维数组,第二次写的代码,因为memset(s,'*',sizeof(s));这里写错了,WA了无数次;

// hdu 2102 A ji hua (xiu gai ban)#include<iostream>#include<queue>#include<cstdio>#include<cstring>using namespace std;#define N 15struct point{int x, y, floor, time;} st;int m, n, t, flag;char s[2][N][N];int move[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};queue<point>q;void bfs();int main(){//freopen("1.in", "r", stdin);int tt;scanf("%d", &tt);while (tt--){int i, j;scanf("%d %d %d", &n, &m, &t);memset(s,'*',sizeof(s));for (i = 0; i < 2; i++){for (j = 0; j < n; j++){scanf("%s", s[i][j]);}}flag = 0;st.x = 0;st.y = 0;st.floor = 0;st.time = 0;bfs();if(flag) printf("YES\n");else printf("NO\n");}}void bfs(){while(!q.empty()){q.pop();}q.push(st);while(!q.empty()){point now = q.front();q.pop();//printf("x %d y %d f %d t %d\n",now.x, now.y, now.floor, now.time);for(int i=0;i<4;i++){point next = now;int xx = now.x, yy = now.y, f = now.floor;//printf("xx %d yy %d\n",xx,yy);xx += move[i][0];yy += move[i][1];next.time++;if(next.time>t) continue;if(xx<0 || xx>=n || yy<0 || yy>=m)continue;if(s[f][xx][yy] == '*') continue;if (s[f][xx][yy] == 'P'){flag = 1;return;}if(s[f][xx][yy]=='#' && s[1-f][xx][yy] == 'P'){flag = 1;return;}if(s[f][xx][yy]=='#' && s[1-f][xx][yy] == '#'){s[0][xx][yy] = '*';s[1][xx][yy] = '*';continue;}if(s[f][xx][yy]=='#' && s[1-f][xx][yy] == '*'){s[0][xx][yy] = '*';s[1][xx][yy] = '*';continue;}if(s[f][xx][yy]=='#' && s[1-f][xx][yy] == '.'){s[f][xx][yy] = '*';s[1-f][xx][yy] = '*';next.floor = 1-f;next.x = xx;next.y = yy;}if(s[f][xx][yy] == '.'){s[f][xx][yy] = '*';next.x = xx;next.y = yy;}q.push(next);}}}


第一次写的代码,还是WA,不知原因啊》》》哭

//hdu 2102 A¼Æ»®#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;struct point{int x, y, floor, time;} st;int m, n, t, flag;char s1[15][15], s2[15][15];int vis1[15][15], vis2[15][15];int move[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};queue<point>q;void bfs();int main(){//freopen("1.in", "r", stdin);int tt;cin >> tt;while (tt--){int i, j ;cin >> n >> m >> t;memset(s1,'*',sizeof(s1));memset(s2,'*',sizeof(s2));for (i = 0; i < n; i++){cin >> s1[i];}for (i = 0; i < n; i++){cin >> s2[i];}for (i = 0; i < n; i++){for (j = 0; j < m; j++){if (s1[i][j] == 'S'){st.x = i;st.y = j;st.floor = 1;st.time = 0;break;}if (s2[i][j] == 'S'){st.x = i;st.y = j;st.floor = 2;st.time = 0;break;}}}memset(vis1, 0, sizeof(vis1));memset(vis2, 0, sizeof(vis2));flag = 0;bfs();if (flag){printf("YES\n");}else{printf("NO\n");}}return 0;}void bfs(){while (!q.empty()){q.pop();}q.push(st);while (!q.empty()){point now = q.front();q.pop();//printf("x %d,y %d, f %d, t %d\n",now.x,now.y,now.floor,now.time);for (int i = 0; i < 4; i++){int xx = now.x, yy = now.y;xx += move[i][0];yy += move[i][1];if (s1[xx][yy] == 'P' && now.floor == 1 && now.time < t){flag = 1;return;}if (s2[xx][yy] == 'P' && now.floor == 2 && now.time < t){flag = 1;return;}if (xx >= 0 && xx < n && yy >= 0 && yy < m && now.time <= t){point next = now;if (now.floor == 1 && !vis1[xx][yy]){if (s1[xx][yy] == '.'){vis1[xx][yy] = 1;next.x = xx;next.y = yy;next.floor = 1;next.time++;q.push(next);}else if (s1[xx][yy] == '#' && s2[xx][yy] != '*' && s2[xx][yy] != '#' && !vis2[xx][yy]){if(s2[xx][yy] == 'P') {flag = 1; return;}else{vis2[xx][yy] = 1;next.x = xx;next.y = yy;next.floor = 2;next.time++;q.push(next);}}}else if (now.floor == 2 && !vis2[xx][yy]){if (s2[xx][yy] == '.'){vis2[xx][yy] = 1;next.x = xx;next.y = yy;next.floor = 2;next.time++;q.push(next);}else if (s2[xx][yy] == '#' && s1[xx][yy] != '*' && s1[xx][yy] != '#' && !vis1[xx][yy]){if(s1[xx][yy] == 'P') {flag = 1;return;}else{vis1[xx][yy] = 1;next.x = xx;next.y = yy;next.floor = 1;next.time++;q.push(next);}}}else{continue;}}}}}


0 0