ICPC Pacific Northwest Regional Contest 2016 C. Buggy Robot

此生再无相见时 提交于 2020-08-06 23:37:34


Buggy Robot  

 

思路:dp[inx][x][y],表示用了前inx个指令后的最小费用。

对于一个指令,我们可以选择不走或者走,其他的我们可以添加四个方向的指令与使用过指令后的dp来比较。

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <queue>
  5 #include <cstring>
  6 
  7 using namespace std;
  8 
  9 const int N = 60;
 10 const int INF = 1e9;
 11 int mv_x[] = {1, -1, 0, 0};
 12 int mv_y[] = {0, 0, -1, 1};
 13 char mp[N][N];
 14 int dp[N][N][N];
 15 char str[N];
 16 int n, m;
 17 
 18 struct node
 19 {
 20     int inx, x, y;
 21 }R, E;
 22 
 23 bool inline check(int x, int y)
 24 {
 25     return x >= 0 && x < n && y >= 0 && y < m;
 26 }
 27 
 28 void bfs()
 29 {
 30     for(int i = 0; i < N; ++i)
 31         for(int j = 0; j < N; ++j)
 32             for(int k = 0; k < N; ++k)
 33                 dp[i][j][k] = INF;
 34 
 35     int len = strlen(str);
 36     queue<node > que; 
 37 
 38     dp[0][R.x][R.y] = 0;
 39     que.push({0, R.x, R.y});
 40 
 41     while(!que.empty()){
 42         node now = que.front();
 43         que.pop();
 44 
 45         for(int p = 0; p < 4; ++p){
 46             int dx = now.x + mv_x[p];
 47             int dy = now.y + mv_y[p];
 48 
 49             if(check(dx, dy) && mp[dx][dy] != '#'){
 50                 if(dp[now.inx][now.x][now.y] + 1 < dp[now.inx][dx][dy]){
 51                     dp[now.inx][dx][dy] = dp[now.inx][now.x][now.y] + 1;
 52                     que.push({now.inx, dx, dy});
 53                 }
 54             }
 55 
 56             if(now.inx < len){
 57                 //不走
 58                 if(dp[now.inx][now.x][now.y] + 1 < dp[now.inx + 1][now.x][now.y]){
 59                     dp[now.inx + 1][now.x][now.y] = dp[now.inx][now.x][now.y] + 1;
 60                     que.push({now.inx + 1, now.x, now.y}); 
 61                 }
 62 
 63                 //
 64                 dx = now.x; 
 65                 dy = now.y;
 66                 if(str[now.inx] == 'L') dy--;
 67                 if(str[now.inx] == 'R') dy++;
 68                 if(str[now.inx] == 'U') dx--;
 69                 if(str[now.inx] == 'D') dx++;
 70                 if(!check(dx,dy) || mp[dx][dy] == '#') dx = now.x, dy = now.y;
 71                 if(dp[now.inx][now.x][now.y] < dp[now.inx + 1][dx][dy]){
 72                     dp[now.inx + 1][dx][dy] = dp[now.inx][now.x][now.y];
 73                     que.push({now.inx + 1, dx, dy}); 
 74                 }                
 75             }
 76         }
 77     }
 78 }
 79 
 80 void solve()
 81 {
 82     scanf("%d%d", &n, &m);
 83     for(int i = 0; i < n; ++i) scanf("%s", mp[i]);
 84     scanf("%s", str);
 85     //cout << "len = " << (strlen(str)) << endl;
 86     
 87     for(int i = 0; i < n; ++i){
 88         for(int j = 0; j < m; ++j){
 89             if(mp[i][j] == 'R'){
 90                 R.x = i; R.y = j;
 91             }
 92             if(mp[i][j] == 'E'){
 93                 E.x = i; E.y = j;
 94             }
 95         }
 96     }
 97 
 98     bfs();
 99 
100     int ans = INF;
101     for(int i = 0; i < N; ++i){
102         ans = min(ans, dp[i][E.x][E.y]);
103     }
104     //printf("ans = %d\n", ans);
105     printf("%d\n", ans);
106 }
107 
108 
109 int main()
110 {
111 
112     solve();
113 
114     return 0;
115 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!