zoukankan      html  css  js  c++  java
  • UVa 10047

    题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=988&mosmsg=Submission+received+with+ID+26578514

    (d[x][y][dir][c) 记录位置,朝向,颜色,(BFS) 求解即可

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 55;
    const int INF = 0x3f3f3f3f;
    
    int n, m; 
    char ch[maxn];
    
    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, 1, 0, -1};
    
    int sx, sy, tx, ty;
    
    int a[maxn][maxn];
    int d[maxn][maxn][10][10];
    int vis[maxn][maxn][10][10];
    int tc[10] = {1, 2, 3, 4, 0};
    int td[2][10] = {{1, 2, 3, 0}, {3, 0, 1, 2}}; // 0 N 1 E 2 S 3 W
    
    struct Node{
    	int x, y, d, c;
    	Node(int x, int y, int d, int c): x(x), y(y), d(d), c(c) {};
    };
    
    int bfs(){
    	memset(vis, 0, sizeof(vis));
    	memset(d, 0x3f, sizeof(d));
    	queue<Node> q;
    	d[sx][sy][0][0] = 0;
    	q.push(Node(sx, sy, 0, 0));
    	
    	while(!q.empty()){
    		Node p = q.front(); q.pop();
    		
    		if(vis[p.x][p.y][p.d][p.c]) continue;
    		vis[p.x][p.y][p.d][p.c] = 1;
    				
    		if(p.x == tx && p.y == ty && p.c == 0){
    			return d[p.x][p.y][p.d][p.c]; 
    		}
    		
    //		int x = p.x + dx[p.d], y = p.y + dy[p.d], c = tc[p.c]; // 移动 
    		int x = p.x + dx[p.d], y = p.y + dy[p.d], c = tc[p.c];
    		if(x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y] && !vis[x][y][p.d][c]){
    			d[x][y][p.d][c] = d[p.x][p.y][p.d][p.c] + 1;
    			q.push(Node(x, y, p.d, c));
    		}
    		
    		for(int i = 0 ; i < 2 ; ++i){ // 转向 
    			int dir = td[i][p.d];
    			if(!vis[p.x][p.y][dir][p.c]){
    				d[p.x][p.y][dir][p.c] = d[p.x][p.y][p.d][p.c] + 1;
    				q.push(Node(p.x, p.y, dir, p.c));
    			}
    		}
    	}
    	
    	return INF;
    }
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	int flag = 0;
    	int kase = 0;
    	while(scanf("%d%d", &n, &m) && (n || m)){
    		if(flag) printf("
    ");
    		flag = 1; 
    		for(int i = 1 ; i <= n ; ++i){
    			scanf("%s", ch + 1);
    			for(int j = 1 ; j <= m ; ++j){
    				if(ch[j] == '#') a[i][j] = 1;
    				else if(ch[j] == '.'){
    					a[i][j] = 0;
    				} else if(ch[j] == 'S'){
    					sx = i, sy = j;
    					a[i][j] = 0;
    				} else{
    					tx = i, ty = j;
    					a[i][j] = 0;
    				}
    			}
    		}
    		
    		int ans = bfs();
    		
    		printf("Case #%d
    ", ++kase); 
    		if(ans != INF) printf("minimum time = %d sec
    ", ans);
    		else {
    			printf("destination not reachable
    ");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    webpack loader和插件的编写原理
    vue和react原理性知识点
    详谈Javascript类与继承
    vue项目中要实现展示markdown文件[转载]
    前端知识总结--2 js部分
    前端知识总结--html
    react相关知识点总结
    优秀文章
    项目部署服务器2
    项目部署服务器
  • 原文地址:https://www.cnblogs.com/tuchen/p/15021950.html
Copyright © 2011-2022 走看看