标签:continue 题意 code tip ace mono pac 搜索 main
kuangbin专题一:N题
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17428 Accepted Submission(s): 5605
/* 不想加注释 ......*/ #include <Cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std ; #define maxn 300 bool visit[maxn][maxn] ; int n , m ; char map[maxn][maxn] ; int result1[maxn][maxn] ; int result2[maxn][maxn] ; int x1 , y1 , x2 , y2 ; int dirx[] = {0 , 0 , -1 , 1 } ; int diry[] = {-1 , 1 , 0 , 0 } ; struct node { int x ; int y ; int step ; }; bool check(int x , int y ){ if(x>=1 && x<= n && y>=1 && y<=m){ return true ; } return false ; } void BFS(int x , int y , int result[][maxn]){ memset(visit , 0 , sizeof(visit)) ; queue<node> Q ; node st ; st.x = x ; st.y = y ; st.step = 0 ; Q.push(st) ; while(!Q.empty()){ node q = Q.front() ; Q.pop() ; if(map[q.x][q.y] == ‘@‘){ result[q.x][q.y] = q.step ; } node turn ; for(int i=0 ; i<4 ; i++){ turn.x = q.x + dirx[i] ; turn.y = q.y + diry[i] ; turn.step = q.step + 1 ; if(check(turn.x , turn.y ) && !visit[turn.x][turn.y] && map[turn.x][turn.y]!=‘#‘ ) { visit[turn.x][turn.y] = 1 ; Q.push(turn) ; } } } return; } int main(){ while(~scanf("%d%d" , &n , &m)){ for(int i=1 ; i<=n ; i++){ for(int j=1 ; j<=m ; j++){ scanf(" %c" , &map[i][j]) ; if(map[i][j] == ‘Y‘){ x1 = i ; y1 = j ; } if(map[i][j] == ‘M‘){ x2 = i ; y2 = j ; } } } memset(result1 , 0 , sizeof(result1)) ; memset(result2 , 0 , sizeof(result2)) ; BFS(x1 , y1 , result1) ; BFS(x2 , y2 , result2) ; int min_num = 1000000 ; for(int i=1 ; i<=n ; i++){ for(int j=1 ; j<= m ; j++){ if(result1[i][j] == 0 || result2[i][j] == 0 || map[i][j] != ‘@‘) continue ; min_num = min(result1[i][j] + result2[i][j] , min_num ) ; } } printf("%d\n" , min_num * 11 ) ; } return 0 ; }
标签:continue 题意 code tip ace mono pac 搜索 main
原文地址:http://www.cnblogs.com/yi-ye-zhi-qiu/p/7648248.html