Sunday, January 5, 2020

POJ. 3009 Curling 2.0

Problem:
http://poj.org/problem?id=3009

Idea:
Looking for the minimum answer in 10 steps by dfs. When the stone reach to goal, it has to stop the search (In the code the 'flag' is for this purpose).

Source:
 int f[22][22];  
 int h, w;  
 int sx, sy, gx, gy;  
 int ans = 100;  
 void dfs(int x, int y, int throws)  
 {  
      if (throws >= 10) return;  
      int xx = x;  
      int yy = y;  
      int flag = 0;  
      //up  
      x = xx;  
      y = yy;  
      flag = 0;  
      while (x >=0 && f[x][y] == 0) {  
           if (x == gx && y == gy) {  
                ans = min(throws + 1, ans);  
                flag = 1;  
                break;  
           }  
           x--;  
      }  
      if (flag==0 && x >= 0 && (x +1) != xx) {  
           x++;  
           f[x - 1][y] = 0;  
           dfs(x, y, throws + 1);  
           f[x - 1][y] = 1;  
      }  
      //down  
      x = xx;  
      y = yy;  
      flag = 0;  
      while (x < h && f[x][y] == 0) {  
           if (x == gx && y == gy) {  
                ans = min(throws + 1, ans);  
                flag = 1;  
                break;  
           }  
           x++;  
      }  
      if (flag == 0 && x < h && (x - 1) != xx) {  
           x--;  
           f[x+1][y] = 0;  
           dfs(x, y, throws + 1);  
           f[x+1][y] = 1;  
      }  
      //right  
      x = xx;  
      y = yy;  
      flag = 0;  
      if (x == 2 && y == 1) {  
           int g = 0;  
      }  
      while (y < w && f[x][y] == 0) {  
           if (x == gx && y == gy) {  
                ans = min(throws + 1, ans);  
                flag = 1;  
                break;  
           }  
           y++;  
      }  
      if (flag == 0 && y < w && (y -1) != yy) {  
           y--;  
           f[x][y + 1] = 0;  
           dfs(x, y, throws + 1);  
           f[x][y + 1] = 1;  
      }  
      //left  
      x = xx;  
      y = yy;  
      flag = 0;  
      while (y >= 0 && f[x][y] == 0) {  
           if (x == gx && y == gy) {  
                ans = min(throws + 1, ans);  
                flag = 1;  
                break;  
           }  
           y--;  
      }  
      if ( flag == 0 && y >= 0 && (y+1) != yy) {  
           y++;  
           f[x][y - 1] = 0;  
           dfs(x, y, throws + 1);  
           f[x][y - 1] = 1;  
      }  
      return;  
 }  
 int main()  
 {  
      while (1) {  
           cin >> w >> h;  
           if (w + h == 0) break;  
           ans = 100;  
           for (int i = 0; i<h; i++)  
                for (int j = 0; j < w; j++) {  
                     cin >> f[i][j];  
                     if (f[i][j] == 2) {  
                          sx = i;  
                          sy = j;  
                          f[i][j] = 0;  
                     }  
                     if (f[i][j] == 3) {  
                          gx = i;  
                          gy = j;  
                          f[i][j] = 0;  
                     }  
                }  
           dfs(sx, sy, 0);  
           if (ans < 100) cout << ans << endl;  
           else cout << -1 << endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment