zoukankan      html  css  js  c++  java
  • Luogu3855 [TJOI2008]Binary Land (BFS)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    
    #else
    
    #define D_e_Line ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    #define stone 2
    #define spider 3
    #define endPlace 9
    
    const int N = 37;
    
    int n, m;
    
    struct MAP{
    	int x_1, y_1, x_2, y_2, step;
    }u, v;
    
    #include<queue>
    
    queue<MAP>q;
    
    int mp[N][N];
    
    int ans = 0x3f3f3f3f;
    
    int walk_1[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    int walk_2[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
    
    int vis[N][N][N][N];
    
    inline void BFS(){
    	vis[u.x_1][u.y_1][u.x_2][u.y_2] = true;
    	q.push(u);
    	while(!q.empty()){
    		u = q.front();
    		q.pop();
    		
    		if(mp[u.x_1][u.y_1] == endPlace && mp[u.x_2][u.y_2] == endPlace){
    			ans = u.step;
    			return;	
    		}
    		
    		R(i,0,3){
    			v.x_1 = u.x_1 + walk_1[i][0];
    			v.y_1 = u.y_1 + walk_1[i][1];
    			v.x_2 = u.x_2 + walk_2[i][0];
    			v.y_2 = u.y_2 + walk_2[i][1];
    			v.step = u.step + 1;
    			if(v.x_1 < 1 || v.x_2 < 1 || v.y_1 < 1 || v.y_2 < 1 || v.x_1 > n || v.x_2 > n || v.y_1 > m || v.y_2 > m) continue;
    	        if(mp[v.x_1][v.y_1] == stone) v.x_1 = u.x_1, v.y_1 = u.y_1;
    	        if(mp[v.x_2][v.y_2] == stone) v.x_2 = u.x_2, v.y_2 = u.y_2;
    	        if(mp[v.x_1][v.y_1] == spider || mp[v.x_2][v.y_2] == spider) continue;
    	        if(vis[v.x_1][v.y_1][v.x_2][v.y_2]) continue;
    	        vis[v.x_1][v.y_1][v.x_2][v.y_2] = 1;
    			q.push(v);
    	 	}
    	}
    
    }
    
    int st[2][2];
    int main(){
    	io >> n >> m;
    	R(i,1,n){
    		char str[57];
    		scanf("%s", str + 1);
    		R(j,1,m){
    			switch(str[j]){
    				case '#' :{
    					mp[i][j] = stone;
    					break;
    				}
    				case 'X' :{
    					mp[i][j] = spider;
    					break;
    				}
    				case 'T' :{
    					mp[i][j] = endPlace;
    					break;
    				}
    				case 'G' :{
    					mp[i][j] = 7;
    					st[0][0] = i, st[0][1] = j;
    					break;
    				}
    				case 'M' :{
    					mp[i][j] = 8;
    					st[1][0] = i, st[1][1] = j;
    					break;
    				}
    				case '.' :{
    					mp[i][j] = 1;
    					break;
    				}
    			}
    		}
    	}
    	
    	
    	
    	u = (MAP){st[0][0], st[0][1], st[1][0], st[1][1], 0};
    	
    	BFS();
    	
    	if(ans == 0x3f3f3f3f){
    		printf("no");
    	}
    	else{
    		printf("%d", ans);
    	}
    	
    	return 0;
    }
    

  • 相关阅读:
    Java动态代理设计模式
    AOP的相关概念
    如何解决表单提交的中文乱码问题
    怎么防止重复提交
    http的响应码200,404,302,500表示的含义分别是?
    JSP三大指令是什么?
    说一下 session 的工作原理?
    session 和 cookie 有什么区别?
    说一下 JSP 的 4 种作用域?
    jsp有哪些内置对象?作用分别是什么?
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11207603.html
Copyright © 2011-2022 走看看