搜索题
来源:互联网 发布:windows触摸屏无法使用 编辑:程序博客网 时间:2024/05/22 17:03
OJ题目 : 点~~
const int Max_N = 25;const int inf = 100000000;int N , M;char table[Max_N][Max_N];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};int minstep[Max_N][Max_N][Max_N][Max_N];struct Point//表示Ball的结构体{ int x; int y; int OK;//1表示已搞定 , 0表示还没搞定 Point(){} Point(int i , int j , int k):x(i),y(j),OK(k){}};struct Node//表示状态的结构体{ Point A; Point B; int step;//本状态的步数 Node(){} Node(Point i , Point j , int s):A(i),B(j),step(s){}};vector<Point> List_Ball;int bfs(Node s){ int i , j , k , t; for(i = 1;i <= N;i++) for(j = 1;j <= M;j++) for(k = 1;k <= N;k++) for(t = 1;t <= M;t++) minstep[i][j][k][t] = inf; queue<Node> que; que.push(s); Node now ; Point newA , newB; while(!que.empty())//bfs { now = que.front(); que.pop(); if(now.A.OK&& now.B.OK)//如果两个Ball都已经进Hole , 成功 , 返回步数 return now.step; int nextAx , nextBx , nextAy , nextBy; for(i = 0;i < 4;i++) { nextAx = now.A.x + dir[i][0]; nextAy = now.A.y + dir[i][1]; nextBx = now.B.x + dir[i][0]; nextBy = now.B.y + dir[i][1]; if(table[nextAx][nextAy] == '*' && table[nextBx][nextBy] == '*')//如果下面两个位置都是墙,则跳过 continue; if(now.A.OK||table[nextAx][nextAy] == '*')//如果A已搞定,或者下个位置是墙,A的位置不变 { nextAx = now.A.x; nextAy = now.A.y; } if(now.B.OK ||table[nextBx][nextBy] == '*')//如果B已搞定,或者下个位置是墙,B的位置不变 { nextBx = now.B.x; nextBy = now.B.y; } if(!now.A.OK && !now.B.OK && nextAx == nextBx && nextAy == nextBy)//如果AB都在,且下个位置重合,则跳过 continue; newA.x = nextAx; newA.y = nextAy; newB.x = nextBx; newB.y = nextBy; if(now.A.OK)//如果A已经搞定,下面考虑B { newA.OK = 1;//A的状态要赋值!! if(table[nextBx][nextBy] == 'H'&& !(nextAx == nextBx && nextAy == nextBy))//如果B的新位置刚好是Hole,且不是A的那个Hole,则B搞定 newB.OK = 1; else newB.OK = 0; } else if(now.B.OK)//如果B已经搞定,下面考虑A { newB.OK = 1;//B的状态要赋值!! if(table[nextAx][nextAy] == 'H' && !(nextBx == nextAx && nextBy == nextAy))//如果A的新位置刚好是Hole,且不是B的那个Hole,则A搞定 newA.OK = 1; else newA.OK = 0; } else//如果A ,B都未搞定 { if(table[nextAx][nextAy] == 'H')//考虑A { newA.OK = 1; } else newA.OK = 0; if(table[nextBx][nextBy] == 'H')//考虑B { newB.OK = 1; } else newB.OK = 0; } if(minstep[newA.x][newA.y][newB.x][newB.y] > now.step + 1)//如果新状态步数比之前的少,则更新 { minstep[newA.x][newA.y][newB.x][newB.y] = now.step + 1; que.push(Node(newA , newB , now.step + 1)); } } } return -1;}int main(){ int Case; cin >> Case; while(Case--) { scanf("%d%d" , &N ,&M); List_Ball.clear(); int i , j; for(i = 1;i <= N;i++) scanf("%s",table[i]+1); for(i = 1;i <= N;i++) for(j = 1;j <= M;j++) if(table[i][j] == 'B') List_Ball.push_back(Point(i , j , 0));//List_Ball保存两个Ball int ans = bfs(Node(List_Ball[0] , List_Ball[1] , 0));//广搜 if(ans == -1) cout << "Sorry , sir , my poor program fails to get an answer." << endl ; else cout << ans << endl; } return 0;}
0 0
- 搜索题
- 搜索题
- 【搜索】两道搜索题
- 搜索题----买鱼
- PKU搜索题--part1
- HDU 1704 (搜索题)
- 人民搜索笔试题:
- POJ 搜索题
- POJ1010-搜索题基础
- 搜索比赛A题
- 搜索比赛B题
- 搜索比赛C题
- 搜索比赛D题
- 搜索比赛E题
- 搜索比赛F题
- POJ 搜索题
- 高级搜索题集
- 经典搜索题
- 在Eclipse中debug源码OpenERP7.0的步骤
- oracle sql语句
- Windows,linux双系统,删除linux.之后恢复损坏的MBR方法
- SVN问题
- jQueryMobile主题修改的若干问题
- 搜索题
- 找出在数组中出现次数超过一半的那个数
- 06 Unity3D摄像机与层--《程序员学Unity3d》
- Apache上部署Django
- HDU--1040 As Easy As A+B
- 几个浏览器的H5测试
- Spring 基础教程之三:JavaBean生命周期
- C++ 文件
- 微软100题-天天做-第18题