DFS poj 3009

来源:互联网 发布:131458软件 编辑:程序博客网 时间:2024/05/16 14:14
Curling 2.0
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7141 Accepted: 2991

Description

On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.


Fig. 1: Example of board (S: start, G: goal)

The movement of the stone obeys the following rules:

  • At the beginning, the stone stands still at the start square.
  • The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
  • When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
  • Once thrown, the stone keeps moving to the same direction until one of the following occurs:
    • The stone hits a block (Fig. 2(b), (c)).
      • The stone stops at the square next to the block it hit.
      • The block disappears.
    • The stone gets out of the board.
      • The game ends in failure.
    • The stone reaches the goal square.
      • The stone stops there and the game ends in success.
  • You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.


Fig. 2: Stone movements

Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).


Fig. 3: The solution for Fig. D-1 and the final board configuration

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

Each dataset is formatted as follows.

the width(=w) and the height(=h) of the board
First row of the board
...
h-th row of the board

The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.

Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

0 vacant square1 block2 start position3 goal position

The dataset for Fig. D-1 is as follows:

6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1

Output

For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

Sample Input

2 13 26 61 0 0 2 1 01 1 0 0 0 00 0 0 0 0 30 0 0 0 0 01 0 0 0 0 10 1 1 1 1 16 11 1 2 1 1 36 11 0 2 1 1 312 12 0 1 1 1 1 1 1 1 1 1 313 12 0 1 1 1 1 1 1 1 1 1 1 30 0

Sample Output

14-1410-1


题意:

一颗珠子从原点(2)开始,最后想要到达终点(3)。

其规则是: 只能往X,Y的方向抛掷,且抛掷后只有遇到障碍(1)或终点才能停下来;且遇到障碍后,障碍会消失,且珠子会往反方向滚一格。

滚出框外算输。且如果相邻即是障碍,则不能朝那个方向抛掷。

求珠子滚到终点所要的步数,如果到达不了或超过10步,则输出-1.


思路:

一道非常明显的DFS题。只是我一开始不是求的最短步数,改了很多次啊很多次。。。而且代码很长。。。


代码实现:
View Code
  1 #include<iostream>  2 #include<string.h>  3 using namespace std;  4 int a[50][50];  5 int ax[50];  6 int ay[50];  7 struct point{  8        int x;  9        int y; 10        }; 11 point start; 12 point end; 13 int bx[4]={0,0,-1,1}; 14 int by[4]={1,-1,0,0}; 15 int n,m; 16 int sumlen=24; 17 int DFS(point v,int len){ 18     if(len>10) 19         return 0; 20     int k; 21     for(k=0;k<4;k++){ 22             if(v.x+bx[k]>0&&v.x+bx[k]<=n&&v.y+by[k]>0&&v.y+by[k]<=m){ 23                        if(a[v.x+bx[k]][v.y+by[k]]!=1) 24                              break; 25                              } 26                              } 27     if(k>=4) 28         return 0; 29     if((v.x==end.x||v.y==end.y)){ 30        int j,g,t1,t2,t3,t4; 31        if(v.y==end.y){ 32           t1=v.x<end.x?v.x:end.x; 33           t2=v.x>end.x?v.x:end.x; 34           for(j=t1+1;j<t2;j++){ 35                   if(a[j][v.y]==1) 36                         break; 37                         } 38           if(j>=t2) 39                return len; 40                         } 41       else{ 42           t3=v.y<end.y?v.y:end.y; 43           t4=v.y>end.y?v.y:end.y; 44           for(g=t3+1;g<t4;g++){ 45                   if(a[v.x][g]==1) 46                         break; 47                         } 48          if(g>=t4)  49               return len; 50                         } 51             }  52     if(ax[v.x]==0&&ay[v.y]==0){ 53         return 0; 54         } 55     if(ax[v.x]!=0){ 56         for(int k=v.y-1;k>0;k--){ 57                 if(k==v.y-1&&a[v.x][k]==1) 58                           break; 59                 if(a[v.x][k]==1&&v.y!=k){ 60                       point temp; 61                       temp.x=v.x; 62                       temp.y=k+1; 63                       a[v.x][k]=0; 64                       ax[temp.x]--; 65                       ay[k]--;                       66                       int temp2=DFS(temp,len+1); 67                              a[v.x][k]=1; 68                              ax[temp.x]++; 69                              if(temp2>0){ 70                              if(sumlen>temp2) 71                                    sumlen=temp2; 72                                    } 73                              ay[k]++; 74                              break;     75                              } 76                         } 77         for(int k=v.y+1;k<=m;k++){ 78                 if(k==v.y+1&&a[v.x][k]==1) 79                           break; 80                 if(a[v.x][k]==1&&v.y!=k){ 81                       point temp; 82                       temp.x=v.x; 83                       temp.y=k-1; 84                       a[v.x][k]=0; 85                       ax[temp.x]--; 86                       ay[k]--;                       87                       int temp2=DFS(temp,len+1); 88                              a[v.x][k]=1; 89                              ax[temp.x]++; 90                              ay[k]++;  91                              if(temp2>0){ 92                                if(sumlen>temp2) 93                                    sumlen=temp2; 94                                    } 95                              break;  96                              } 97                              } 98                         } 99     if(ay[v.y]!=0){100         for(int g=v.x-1;g>0;g--){101                 if(g==v.x-1&&a[g][v.y]==1)102                           break;103                 if(a[g][v.y]==1&&v.x!=g){104                      point temp1;105                      temp1.x=g+1;106                      temp1.y=v.y;107                      a[g][v.y]=0;108                      ax[g]--;109                      ay[temp1.y]--;    110                      int temp3=DFS(temp1,len+1);111                             a[g][v.y]=1;112                             ax[g]++;113                             ay[temp1.y]++;114                             if(temp3>0){115                              if(sumlen>temp3)116                                    sumlen=temp3;117                                    }118                             break;    119                             }120                             }121     for(int k=v.x+1;k<=n;k++){122                 if(k==v.x+1&&a[k][v.y]==1)123                           break;124                 if(a[k][v.y]==1&&v.x!=k){125                       point temp;126                       temp.x=k-1;127                       temp.y=v.y;128                       a[k][v.y]=0;129                       ax[k]--;130                       ay[temp.y]--;                      131                       int temp2=DFS(temp,len+1);132                              a[k][v.y]=1;133                              ax[k]++;134                              ay[temp.y]++;135                              if(temp2>0) {136                               if(sumlen>temp2)137                                    sumlen=temp2;138                                    }139                              break;   140                              }141                         }142                             }143   return sumlen;144 }                    145 int main(){146     while(cin>>m>>n){147        if(m==0&&n==0)148              break;149        sumlen=24;150        memset(a,0,sizeof(a));151        memset(ax,0,sizeof(ax));152        memset(ay,0,sizeof(ay));153        for(int i=1;i<=n;i++){154                for(int j=1;j<=m;j++){155                        cin>>a[i][j];156                        if(a[i][j]==2){157                             start.x=i;158                             start.y=j;159                             }160                        if(a[i][j]==3){161                             end.x=i;162                             end.y=j;163                             }164                        else if(a[i][j]==1){165                             ax[i]++;166                             ay[j]++;167                             }168                             }169                             }170        int sum=DFS(start,1);171        if(sum==0||sum>10)172              cout<<"-1"<<endl;173        else174              cout<<sum<<endl;175              }176     return 0;177 }178