- #include <bits/stdc++.h>
- using namespace std;
- int m, n;
- string grid[21];
- bool vis[21][21];
- int dx[] = {0, 0, 1, -1};
- int dy[] = {1, -1, 0, 0};
- int dis[21][21];
- bool valid(int i, int j){
- if(i<0 && i >=n && j <0 && j >=m) return false;
- if(grid[i][j] == 'm' || grid[i][j] == '#') return false;
- return true;
- }
- void bfs( pair<int, int> s){
- memset(vis,false, sizeof(vis));
- memset(dis, 0, sizeof(dis));
- list< pair<int, int> >q;
- q.push_back(s);
- vis[s.first][s.second]= true;
- dis[s.first][s.second] = 0;
- while(!q.empty()){
- pair <int, int> from = q.front();
- vis[from.first][from.second] = true;
- q.pop_front();
- for(int i = 0; i <4; i++){
- pair<int, int> to;
- to.first = from.first + dx[i];
- to.second = from.second + dy[i];
- if(!valid(to.first, to.second)) continue;
- if(vis[to.first][to.second]) continue;
- vis[to.first][to.second] = true;
- q.push_back(to);
- dis[to.first][to.second] = dis[from.first][from.second] + 1;
- }
- }
- }
- int main(){
- int ts;
- scanf("%d", &ts);
- for(int p= 1; p <=ts; p++){
- pair <int, int> a, b, c, h;
- scanf("%d%d", &n,&m);
- for(int i = 0; i <n; i++){
- //scanf("%s", grid[i]);
- cin>>grid[i];
- for(int j = 0; j <grid[i].length(); j++){
- if(grid[i][j] == 'a') {
- a.first = i;
- a.second = j;
- }
- if(grid[i][j] == 'b') {
- b.first = i;
- b.second = j;
- }
- if(grid[i][j] == 'c') {
- c.first = i;
- c.second = j;
- }
- if(grid[i][j] == 'h') {
- h.first = i;
- h.second = j;
- }
- }
- }
- /*
- for(int i = 0; i <n; i++){
- cout<<grid[i]<<endl;
- //cout<<endl;
- }
- */
- bfs(h);
- int ans = max(max(dis[a.first][a.second], dis[b.first][b.second]),dis[c.first][c.second] );
- printf("Case %d: %d\n", p, ans);
- /*
- cout<<endl;
- for(int i = 0; i <n; i++){
- for(int j = 0; j <m; j++){
- cout<<dis[i][j]<<" ";
- }
- cout<<endl;
- }
- */
- }
- return 0;
- }
Saturday, 29 July 2017
Lightoj 1238 - Power Puff Girls
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
-
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
No comments:
Post a Comment