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

【题意】

在这里输入题意

【题解】

设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;
}

最新文章

  1. Linux ubuntu安装
  2. 《DSP using MATLAB》 示例Example4.1
  3. linux 开启wifi热点
  4. c 函数及指针学习 10
  5. spark - 从HDFS加载文件并分析
  6. two sets of Qt binaries into the same process的解决办法
  7. CoreJavaE10V1P3.9 第3章 Java的基本编程结构-3.9 大数值(Big Numbers)
  8. Linux Date命令学习笔记
  9. python自动化运维:系统基础信息模块
  10. while(true)应用 之 实现自己的消息队列
  11. ubuntu12.04destdrop删除不必要的软件
  12. 坑:JavaScript 中 操作符“==” 和“===” 的区别
  13. C#2.0 迭代器
  14. C语言之概述
  15. Mock测试接口
  16. bootstrap boosting bagging辨析
  17. python面向对象基本概念(OOP)
  18. Centos6.8配置HTTPS
  19. css 悬浮框
  20. poi excel

热门文章

  1. [Chromium文档转载,第001章] Mojo Migration Guide
  2. Felx之HTTPService
  3. java实现折半查找
  4. 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales
  5. Template template parameter(模板參数) example
  6. bzoj2938【Poi2000】病毒
  7. js---12对象创建方式,构造器,原型
  8. js ---- 函数防抖
  9. BZOJ离线版
  10. 深入理解Android(1)——理解Android中的JNI(上)