• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

2013 Asia Hangzhou Regional Contest B

Stealing Harry Potter's Precious

Time Limit: 2000/1000 MS (Java/Others)

    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 135    Accepted Submission(s): 72

Problem Description
  Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:
  Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers' properties, so they live in the indestructible rooms and put customers' properties in vulnerable rooms. Harry Potter's precious are also put in some vulnerable rooms. Dudely wants to steal Harry's things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can't access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry's precious are. He wants to collect all Harry's precious by as less steps as possible. Moving from one room to another adjacent room is called a 'step'. Dudely doesn't want to get out of the bank before he collects all Harry's things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry's precious.
 
Input
  There are several test cases.   In each test cases:   The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 100).   Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, '.' means a vulnerable room, and the only '@' means the vulnerable room from which Dudely starts to move.   The next line is an integer K ( 0 < K <= 4), indicating there are K Harry Potter's precious in the bank.   In next K lines, each line describes the position of a Harry Potter's precious by two integers X and Y, meaning that there is a precious in room (X,Y).   The input ends with N = 0 and M = 0
 
Output
  For each test case, print the minimum number of steps Dudely must take. If Dudely can't get all Harry's things, print -1.
 
Sample Input
2 3
##@
#.#
1
2 2
4 4
#@##
....
####
....
2
2 1
2 4
0 0
 
Sample Output
-1
5
 
Source
2013 Asia Hangzhou Regional Contest
 
Recommend
We have carefully selected several similar problems for you:  4780 4779 4778 4777 4776 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <stack>
 4 #include <queue>
 5 #include <vector>
 6 #include <string>
 7 #include <cmath>
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <iostream>
12 #include <algorithm>
13 using namespace std;
14 #define maxn 202
15 #define ll long long
16 #define mod 1000000007
17 #define INF 0x7fffffff
18 #define eps 1e-8
19 int n, m, k,sx,sy,ans;
20 char s[maxn][maxn];
21 int  vis[maxn];
22 int a[maxn][maxn];
23 int d[maxn][maxn];
24 int x[maxn];
25 int y[maxn];
26 int mi, flag;
27 int dx[4] = { 0,1, 0, -1 };
28 int dy[4] = { 1,0, -1, 0 };
29 int ok(int x, int y){
30     if (x <= 0 || x > n || y <= 0 || y > m || a[x][y] == -1)return 0;
31     else return 1;
32 }
33 int bfs(int sx,int sy,int tx,int ty){
34     queue<int>q,p;
35     q.push(sx); p.push(sy);
36     while (!q.empty()){
37         int x = q.front(), y = p.front();
38         q.pop(); p.pop();
39         for (int i = 0; i < 4; i++){
40             int nx = x + dx[i];
41             int ny = y + dy[i];
42             if (ok(nx,ny)&&d[nx][ny]>d[x][y]+1){
43                 d[nx][ny] = d[x][y] + 1;
44                 q.push(nx); p.push(ny);
45             }
46         }
47     }
48     return d[tx][ty];
49 }
50 void dfs(int u,int t){
51     for (int i = 1; i <= k; i++){
52         if (!vis[i]){
53             vis[i] = 1;
54             for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)d[i][j] = INF/1000;
55             d[x[u]][y[u]] = 0;
56             int xx = bfs(x[u], y[u],x[i],y[i]);
57             ans += xx;
58             if (t + 2 == k)mi = min(mi, ans);
59             dfs(i,t+1);
60             ans -= xx;
61             vis[i] = 0;
62         }
63     }
64 }
65 int main(){
66     int t;
67     /*while (t--){
68         scanf("%d", &n);
69     }*/
70     while (scanf("%d%d", &n, &m)){
71         if (n == 0 || m == 0)break;
72         k = 0;
73         mi = INF;
74         int ans = 0;
75         memset(a, 0, sizeof a);
76         for (int i = 1; i <= n; ++i){
77             scanf("%s", s[i]);
78             for (int j = 1; j <= m; ++j){
79                 if (s[i][j-1] == '@'){ x[k] = i; y[k++] = j; continue; }
80                 if (s[i][j-1] == '#')a[i][j] = -1;
81             }
82         }
83         int z;
84         scanf("%d", &z);
85         while (z--){
86             scanf("%d%d", &x[k], &y[k]);
87             k++;
88         }
89         vis[0] = 1;
90         dfs(0,0);
91         if (mi == INF||mi==INF/1000||mi==0)printf("-1\n");
92         else printf("%d\n", mi);
93     }
94     return 0;
95 }
View Code
posted @ 2013-11-10 01:23  HaibaraAi  阅读(136)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3