POJ 2688 Cleaning Robot (tsp problem)

Source: Internet
Author: User

Cleaning Robot
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4073 Accepted: 1659

Description

Here, we want to solve path planning for a mobile robot cleaning a rectangular the floor with furniture.

Consider the paved with square tiles whose size fits the cleaning robot (1 * 1). There is ' clean tiles ' and ' dirty tiles ', and the robot can change a ' dirty tiles ' to a ' clean tiles ' by visiting the tile. Also There may is some obstacles (furniture) whose size fits a tile in the guest. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to a adjacent tile with one move. The tile onto which the robot moves must is one of four tiles (i.e., east, west, north or south) adjacent to the tile wher E the robot is present. The robot may visit a tiles twice or more.

Your task is-to-write a program which computes the minimum number of moves for the robot-change all ' dirty tiles ' to ' C Lean tiles ', if ever possible.

Input

The input consists of multiple maps, each representing the size and arrangement of the. 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 sides of the "the floor of the" in terms of widths of floor tiles. W and H is less than or equal to 20. The character Cyx represents what's 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 the 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...*.........*...* ... 13.......x..........o...x....*.........x..............x..............x......................xxxxx.....xxxxx ................. x..............x..............x.........*....x....*.........x ..... Ten 10............o..........................................xxxxx.....x.........x.*.......x.........x .... 0 0

Sample Output

849-1

______

So stupid, I didn't see the difference in this problem, I thought it was a search.

And then the sample is over ...

The result is obviously WA ...

Then found that the problem should be TSP.

The solution is to run the BFS first,

For all the dirty points and the starting point, get the distance between the two points.

Then run DFS, enumerate all the combinations, and update the answers.

Good night

/************************************************************************* > File name:code/poj/rr2688.cpp &G T AUTHOR:111QQZ > Email: [email protected] > Created time:2015 August 16 Sunday 03:39 34 seconds ************************* ***********************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intINF =0x7fffffff;Const intn= -;intw,h;CharMaze[n][n];intDist[n][n];intCnt//number of robots and dirty landintTag[n][n];//MarkBOOLVist[n][n];structnode{intx, y; intstep; BOOLok () {if(x<1|| x>h| | y<1|| y>w| | vist[x][y]| | maze[x][y]=='x')        return false; return true; }}pos[n*N];node robot;intdir[4][2]={0,-1,0,1,-1,0,1,0};voidBFS (node p,intPO) {VIST[P.X][P.Y]=1; Queue<node>Q;    Q.push (P);  while(!Q.empty ()) {Node cur=Q.front ();        Q.pop (); if(maze[cur.x][cur.y]=='o'|| maze[cur.x][cur.y]=='*') Dist[po][tag[cur.x][cur.y]]=Cur.step;        Node Next; Next.step=cur.step+1;  for(intI=0;i<4; i++) {Next.x=cur.x+dir[i][0]; Next.y=cur.y+dir[i][1]; if(!Next.ok ())Continue;            Q.push (next); VIST[NEXT.X][NEXT.Y]=1; }    }}intans=inf;BOOLVis[n];voidDfsintXintStepints) {    if(step==CNT) {        if(s<ans) ans=s; return ; }    if(s>ans)return ;  for(intj=1; j<=cnt;j++)    {        if(Vis[j])Continue; VIS[J]=1; DFS (J,step+1, s+Dist[x][j]); VIS[J]=0; }}intMain () { while(~SCANF ("%d%d",&w,&h)) {if(w==0&&h==0)             Break; //GetChar ();Cnt=0; memset (POS,0,sizeof(POS)); memset (Tag,0,sizeof(tag));  for(intI=1; i<=h;i++) {scanf ("%s", maze[i]+1);  for(intj=1; j<=w;j++)                if(maze[i][j]=='o') {pos[++cnt].x=i; Pos[cnt].y=J; Robot.x=i; Robot.y=J; TAG[I][J]=CNT; }                Else if(maze[i][j]=='*') {pos[++cnt].x=i; Pos[cnt].y=J; TAG[I][J]=CNT; }        }         for(intI=1; i<=cnt;i++)             for(intj=1; j<=cnt;j++)                if(I! =j) Dist[i][j]=inf; ElseDist[i][j]=0;  for(intI=1; i<=cnt;i++) {memset (vist,0,sizeof(vist)); Pos[i].step=0;        BFS (Pos[i],i); }        BOOLflag=1;  for(intI=1; i<=cnt && flag;i++)             for(intj=1; j<=cnt && flag;j++)                if(dist[i][j]==INF) flag=0; if(flag==0) {puts ("-1"); Continue; } memset (Vis,0,sizeof(VIS)); VIS[TAG[ROBOT.X][ROBOT.Y]]=1; Ans=inf; DFS (TAG[ROBOT.X][ROBOT.Y),1,0); printf ("%d\n", ans); }    return 0;}

POJ 2688 Cleaning Robot (tsp problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.