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;
    }
    
  • 相关阅读:
    [数据结构]直接插入排序
    隐藏小程序scroll-view组件的滚动条
    当 uni-app 遇见 vscode
    npm(你怕吗) 全局安装与本地安装、开发依赖和生产依赖
    Vue-resource的使用
    spy-debugger调试、抓包工具
    一个小时学会Git
    flex布局踩过的坑
    Html5移动端布局及(rem布局)页面自适应布局详解
    使用vscode自动编译less
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7875307.html
Copyright © 2011-2022 走看看