【习题 6-5 UVA-1600】Patrol Robot
2024-10-01 22:39:14
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
设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;
}
最新文章
- Linux ubuntu安装
- 《DSP using MATLAB》 示例Example4.1
- linux 开启wifi热点
- c 函数及指针学习 10
- spark - 从HDFS加载文件并分析
- two sets of Qt binaries into the same process的解决办法
- CoreJavaE10V1P3.9 第3章 Java的基本编程结构-3.9 大数值(Big Numbers)
- Linux Date命令学习笔记
- python自动化运维:系统基础信息模块
- while(true)应用 之 实现自己的消息队列
- ubuntu12.04destdrop删除不必要的软件
- 坑:JavaScript 中 操作符“==” 和“===” 的区别
- C#2.0 迭代器
- C语言之概述
- Mock测试接口
- bootstrap boosting bagging辨析
- python面向对象基本概念(OOP)
- Centos6.8配置HTTPS
- css 悬浮框
- poi excel