zoukankan      html  css  js  c++  java
  • 【习题 6-5 UVA-1600】Patrol Robot

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路 bfs一下就ok

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
    */
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 20;
    
    int dx[4] = {0,0,1,-1};
    int dy[4] = {1,-1,0,0};
    
    int k,n,m;
    int a[N+10][N+10],dis[N+10][N+10][N+10];
    queue <pair<int,pair<int,int> > > dl;
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("F:\c++source\rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
    	int T;
    	cin >> T;
    	while (T--){
    		cin >> n >> m;
    		cin >> k;
    		for (int i = 1;i <= n;i++)
    			for (int j = 1;j <= m;j++)
    				cin >> a[i][j];
    		memset(dis,255,sizeof dis);
    		dis[1][1][0] = 0;
    		dl.push(make_pair(1,make_pair(1,0)));
    		while (!dl.empty()){
    			pair <int,pair<int,int> > temp = dl.front();
    			int x = temp.first,y = temp.second.first,num = temp.second.second;
    			dl.pop();
    			for (int i = 0;i < 4;i++){
    			 	int tx = x+dx[i],ty = y + dy[i];
    			 	if (tx >=1 && tx <= n && ty >=1 && ty <= m){
    			 	 	if (a[tx][ty]){
    			 	 	 	int num1 = num+1;
    			 	 	 	if (num1 <= k){
    			 	 	 	 	if (dis[tx][ty][num1]==-1){
    			 	 	 	 	 	dis[tx][ty][num1] = dis[x][y][num] + 1;
    			 	 	 	 	 	dl.push(make_pair(tx,make_pair(ty,num1)));
    			 	 	 		}
    			 	 	 	}
    			 	 	}else{
    			 	 	 	int num1 = 0;
    			 	 	 	if (dis[tx][ty][num1]==-1){
    						 	dis[tx][ty][num1] = dis[x][y][num] + 1;
    			 	 	 	 	dl.push(make_pair(tx,make_pair(ty,num1)));			 	 	 	 	
    			 	 	 	}
    			 	 	}
    			 	}
    			}
    		}
    		int ans = -1;
    		for (int i = 0;i <= k;i++)
    			if (dis[n][m][i]!=-1){
    				if (ans==-1){
    				 	ans = dis[n][m][i];
    				}else{
    				 	ans = min(ans,dis[n][m][i]);
    				}
    			}
    		cout << ans << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    使用AOP 实现Redis缓存注解,支持SPEL
    springmvc 双亲上下文导致的 No mapping found for HTTP request
    调用iframe 中的js[兼容各种浏览器]
    NIO学习:异步IO实例
    byte数组与对象之间的相互转换
    NIO学习:buffer读入与写出(文件复制示例)
    NIO学习:使用Channel、Buffer写入文件
    使用摘要流获取文件的MD5
    ActiveMQ入门实例
    获取java项目 classpath目录
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7875307.html
Copyright © 2011-2022 走看看