POJ 2688 FOJ 1634 Cleaning Robot
来源:互联网 发布:style 用js赋值怎么写 编辑:程序博客网 时间:2024/06/05 06:52
Cleaning Robot
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 936 | Accepted: 391 |
Description
Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture.
Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more.
Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.
Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more.
Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.
Input
The input consists of multiple maps, each representing the size and arrangement of the room. A map is given in the following format.
w h
c11 c12 c13 ... c1w
c21 c22 c23 ... c2w
...
ch1 ch2 ch3 ... chw
The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows.
'.' : a clean tile
'*' : a dirty tile
'x' : a piece of furniture (obstacle)
'o' : the robot (initial position)
In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'.
The end of the input is indicated by a line containing two zeros.
w h
c11 c12 c13 ... c1w
c21 c22 c23 ... c2w
...
ch1 ch2 ch3 ... chw
The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows.
'.' : a clean tile
'*' : a dirty tile
'x' : a piece of furniture (obstacle)
'o' : the robot (initial position)
In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'.
The end of the input is indicated by a line containing two zeros.
Output
For each map, your program should output a line containing the minimum number of moves. If the map includes 'dirty tiles' which the robot cannot reach, your program should output -1.
Sample Input
7 5........o...*.........*...*........15 13.......x..........o...x....*.........x..............x..............x......................xxxxx.....xxxxx......................x..............x..............x.........*....x....*.........x.......10 10............o..........................................xxxxx.....x.........x.*.......x.........x....0 0
Sample Output
849-1
Source
很好玩的题目,感觉很实用....
1.bfs,算出2点之间的所有距离
2.dfs得到结果
通过这题偶知道了居然可以这样dfs....看来以前还是太菜了也............
- #include <iostream>
- #include <queue>
- #include <algorithm>
- using namespace std;
- #define inf 1000000000
- const int dx[4]={0,1,0,-1};
- const int dy[4]={1,0,-1,0};
- int n,m,l,res,mm;
- char map[21][21],a[12];
- int grap[21][21];
- bool f[12];
- struct _point
- {
- int x,y;
- int step;
- bool operator==(const _point& a) const
- {
- return x==a.x&&y==a.y;
- }
- }s,e[12],temp;
- int dis(_point s,_point des)
- {
- bool flag[21][21];
- int i;
- queue<_point> q;
- memset(flag,false,sizeof(flag));
- flag[s.x][s.y]=true;
- s.step=0;
- q.push(s);
- while(!q.empty())
- {
- for(i=0;i<4;i++)
- {
- temp=q.front();
- temp.x+=dx[i];
- temp.y+=dy[i];
- if(flag[temp.x][temp.y]||map[temp.x][temp.y]=='x'||temp.x<1||temp.y<1||temp.x>n||temp.y>m) continue;
- temp.step++;
- flag[temp.x][temp.y]=true;
- q.push(temp);
- if(temp==des)
- return temp.step;
- }
- q.pop();
- }
- return -1;
- }
- void dfs(int t, int n, int pathlen)
- {
- if( n >= l ){
- if( pathlen < res )
- res = pathlen;
- return;
- }
- int i;
- for(i=1;i<=l; i++){
- if( pathlen + grap[t][i] >= res ) continue;
- if(!f[i]&&grap[t][i]!=inf){
- f[i] = true;
- dfs(i, n+1, pathlen + grap[t][i]);
- f[i] = false;
- }
- }
- }
- int main()
- {
- int i,j,k,t;
- bool flag;
- while(scanf("%d%d",&m,&n)!=EOF,m||n)
- {
- getchar();
- l=k=0;
- flag=false;
- for(i=0;i<=11;i++)
- for(j=0;j<=11;j++)
- grap[i][j]=inf;
- for(i=0;i<=n+1;i++)
- map[i][0]=map[i][m+1]='.';
- for(i=0;i<=m+1;i++)
- map[0][i]=map[n+1][i]='.';
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- scanf("%c",&map[i][j]);
- if(map[i][j]=='o')
- s.x=i,s.y=j;
- if(map[i][j]=='*')
- e[l].x=i,e[l++].y=j;
- }
- getchar();
- }
- for(i=l;i>=1;i--)
- e[i]=e[i-1];
- e[0]=s;
- l++;
- for(i=0;i<l&&!flag;i++)
- for(j=0;j<l;j++)
- {
- if(i==j) continue;
- t=dis(e[i],e[j]);
- grap[i][j]=t;
- grap[j][i]=t;
- if(t==-1) {flag=true;break;}
- }
- if(flag)
- {
- cout<<-1<<endl;
- continue;
- }
- res=(1<<20);
- memset(f, 0, sizeof(f));
- f[0]=true;
- dfs(0,1,0);
- printf("%d/n",res);
- }
- }
- POJ 2688 FOJ 1634 Cleaning Robot
- poj 2688 Cleaning Robot
- POJ - 2688 Cleaning Robot
- poj 2688 Cleaning Robot
- POJ 2688 Cleaning Robot
- POJ 2688 Cleaning Robot
- poj 2688 Cleaning Robot
- POJ 2688 Cleaning Robot(状压dp+bfs)
- POJ 2688 Cleaning Robot TSP 问题;
- POJ-2688:Cleaning Robot(bfs预处理+dfs)
- poj 2688 Cleaning Robot BFS+DFS(TSP问题)
- Cleaning Robot
- Cleaning Robot development note
- Cleaning Robot development note
- POJ2688 Cleaning Robot
- FOJ 2200 cleaning(环形dp)
- PKU2688 Cleaning Robot(BFS + DFS)
- Cleaning Robot-dfs+bfs+tsp
- 奥运公交专线 K16 路线图
- QQ不加好友临时会话聊天代码
- 软件安装卸载
- 基于构件的嵌入式系统软件设计
- LR日志
- POJ 2688 FOJ 1634 Cleaning Robot
- Delphi关闭程序前确认
- I、p、B 帧编码的基本流程
- DWORD struct _timeb
- 嵌入式系统软件中的自动化工具设计
- 如何减少SQL Server死锁发生的情况
- 奥运公交专线 K17 路线图
- 在SQL Server中使用索引的技巧
- Macromedia Flash 8 ActionScript: Training from the Source