LightOJ 1055Going Together (暴力搜索……繁琐)
来源:互联网 发布:如何使用花生壳域名 编辑:程序博客网 时间:2024/06/15 21:47
题目
一张n*n的格子图,上面有空地、障碍物、三个出口和三个小人;每次一条指令让他们往四个方向移动一格,不能动的原地不动,动的就移动一格;一个格子上最多同时只有一个人
思路
暴力bfs
const int dx[] = {0, -1, 0, 1};const int dy[] = {1, 0, -1, 0};bool mark[11][11][11][11][11][11];char mp[12][12];int n;struct node { ii o1, o2, o3; int t; node(ii _a, ii _b, ii _c, int _t) { o1 = _a; o2 = _b; o3 = _c; t = _t; }};queue<node> que;void setmark(ii t1, ii t2, ii t3) { mark[t1.first][t1.second][t2.first][t2.second][t3.first][t3.second] = true;}bool check(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n && mp[x][y] != '#');}int bfs(ii& t1, ii& t2, ii& t3) { while(!que.empty()) que.pop(); memset(mark, false, sizeof mark); que.push(node(t1, t2, t3, 0)); setmark(t1, t2, t3); int ans = -1; while(!que.empty()) { node now = que.front(); que.pop(); int x1 = now.o1.first, y1 = now.o1.second; int x2 = now.o2.first, y2 = now.o2.second; int x3 = now.o3.first, y3 = now.o3.second; int t = now.t; if (mp[x1][y1] == 'X' && mp[x2][y2] == 'X' && mp[x3][y3] == 'X') { ans = t; break; } for (int i = 0;i < 4;++i) { bool flag1 = false, flag2 = false, flag3 = false; int _x1 = x1 + dx[i]; int _y1 = y1 + dy[i]; if (!check(_x1, _y1)) { _x1 = x1; _y1 = y1; flag1 = true; } int _x2 = x2 + dx[i]; int _y2 = y2 + dy[i]; if (!check(_x2, _y2)) { _x2 = x2; _y2 = y2; flag2 = true; } int _x3 = x3 + dx[i]; int _y3 = y3 + dy[i]; if (!check(_x3, _y3)) { _x3 = x3; _y3 = y3; flag3 = true; } if (flag1 && flag2 && flag3) { if (mark[_x1][_y1][_x2][_y2][_x3][_y3]) continue; mark[_x1][_y1][_x2][_y2][_x3][_y3] = true; que.push(node(ii(_x1, _y1), ii(_x2, _y2), ii(_x3, _y3), t + 1)); }else { while (1) { bool flag = 0; if (!flag1 && flag2 && _x1 == _x2 && _y1 == _y2) { _x1 = x1, _y1 = y1; flag1 = 1; flag = 1; } if (flag1 && !flag2 && _x1 == _x2 && _y1 == _y2) { _x2 = x2, _y2 = y2; flag2 = 1; flag = 1; } if (!flag1 && flag3 && _x1 == _x3 && _y1 == _y3) { _x1 = x1, _y1 = y1; flag1 = 1; flag = 1; } if (flag1 && !flag3 && _x1 == _x3 && _y1 == _y3) { _x3 = x3, _y3 = y3; flag3 = 1; flag = 1; } if (!flag2 && flag3 && _x2 == _x3 && _y2 == _y3) { _x2 = x2, _y2 = y2; flag2 = 1; flag = 1; } if (flag2 && !flag3 && _x2 == _x3 && _y2 == _y3) { _x3 = x3, _y3 = y3; flag3 = 1; flag = 1; } if (!flag) { break; } } if (mark[_x1][_y1][_x2][_y2][_x3][_y3]) { continue; } mark[_x1][_y1][_x2][_y2][_x3][_y3] = true; que.push(node(ii(_x1, _y1), ii(_x2, _y2), ii(_x3, _y3), t + 1)); } } } return ans;}int main(int argc, const char * argv[]){ // freopen("/Users/jamesqi/Desktop/in.txt","r",stdin); // freopen("/Users/jamesqi/Desktop/out.txt","w",stdout); // ios::sync_with_stdio(false); // cout.sync_with_stdio(false); // cin.sync_with_stdio(false); int kase;cin >> kase; while(kase--) { cin >> n; Rep(i, 1, n) scanf("%s", mp[i] + 1); ii t1, t2, t3; Rep(i, 1, n) Rep(j, 1, n) { if (mp[i][j] == 'A') t1 = ii(i, j); if (mp[i][j] == 'B') t2 = ii(i, j); if (mp[i][j] == 'C') t3 = ii(i, j); } int ans = bfs(t1, t2, t3); if (ans == -1) printf("Case %d: trapped\n", ++nCase); else printf("Case %d: %d\n", ++nCase, ans); } // showtime; return 0;}
0 0
- LightOJ 1055Going Together (暴力搜索……繁琐)
- Going Together
- HDU 3131 One…Two…Five! (暴力搜索)
- 搜索(字符串暴力)
- Lightoj 1148 - Mad Counting (暴力分块)
- LightOJ 1292 Laser Shot(暴力)
- 【LightOJ】1058 - Parallelogram Counting(暴力计数)
- LightOJ-1042---Secret Origins (贪心+暴力)
- lightOJ 1100 【巧妙暴力】
- lightoj 1035【暴力】
- LightOJ 1307【暴力 二分】
- FLIP GAME(暴力搜索)
- 【USACO】Wormholes(暴力搜索)
- 225 - Golygons(暴力搜索)
- 蓝桥杯--带分数(暴力搜索)
- The Clocks(暴力搜索)
- 诸侯安置(暴力搜索)
- 暴力搜索
- 最长上升子序列 动态规划 dp
- 使用css设置img标签靠左、靠右、居中
- 了解计算机系统
- Runtime的几个小例子(含Demo)
- 准备把以前学习深度学习的资料整理共享出来
- LightOJ 1055Going Together (暴力搜索……繁琐)
- Android studio使用androidannotations所出现的问题
- Cordova-----1、搭建Cordova开发环境
- tr命令
- mysql 优化
- PLAN_HASH_VALUE - How equal (and stable?) are your execution plans - part 1
- 深拷贝与浅拷贝
- Java基础面试题
- 高校腐败频发