UVa 1600(未通过)

#include <iostream>
 #include <cstring>
 using namespace std;
 int m,n,k,map[23][23],flag=1,minnum=9999999;
 int dx[]= {-1,1,0,0};
 int dy[]= {0,0,-1,1};
 void print () {
  for(int i=1; i<=m; i++) {
   for(int j=1; j<=n; j++) {
    cout<<map[i][j]<<" ";
   }
   cout<<endl;
  }
  cout<<endl;
 }
 int avil(int x,int y,int tk) {
  if(map[x][y]==-1||x>m||x<=0||y>n||y<=0||(tk==0&&map[x][y]==1)) return 0;
  else return 1;
 }
 //void dfs(int tk,int x,int y,int counter)
 //{
 // int tempk,tempx,tempy,tempc,tempm;
 // //tempm=map[x][y];
 // for(int i=0;i<=3;i++)
 // {
 //  tempx=x+dx[i],tempy=y+dy[i];
 //  if(avil(tempx,tempy,tk)==1)
 //  {
 //   tempm=map[tempx][tempy];
 //   tempc=counter+1;
 //   if(map[tempx][tempy]==1) tempk=tk-1;
 //   else tempk=k;
 //   map[tempx][tempy]=-1;
 //   if(tempx==m&&tempy==n)
 //   {
 //    if(counter<=minnum) minnum=tempc;
     if(tempc==8){
     cout<<"this is"<<tempc<<endl;
     print();}
 //    flag=0;
 //   }
 //   else dfs(tempk,tempx,tempy,tempc);
 //   map[tempx][tempy]=tempm;
 //  }
 // }
 //}
 void bfs()
 {
  	int A[500][4];
  	int head=1,tail=1;
  	A[head][0]=A[head][1]=1;
  	A[head][2]=0;
  	A[head][3]=k;
  	map[1][1]=-1;
  	tail++;
  	while(head<tail)
  	{
   		head++;
   		int tempx,tempy;
   		for(int i=0;i<=3;i++)
   		{
    		tempx=A[head-1][0]+dx[i];
    		tempy=A[head-1][1]+dy[i];
    		if(avil(tempx,tempy,A[head-1][3])==1)
    		{
     			A[tail][0]=tempx,A[tail][1]=tempy;
     			A[tail][2]=A[head-1][2]+1;
     			if(map[tempx][tempy]==1) A[tail][3]=A[head-1][3]-1;
     			else A[tail][3]=k;
     			map[tempx][tempy]=-1;
     			if(tempx==m&&tempy==n)
     			{
      				cout<<A[tail][2]<<endl;
      			return;
     			}
     			tail++;
    		}
   		}
  	}
  	cout<<"-1"<<endl;
 }
 int main() 
 {
  	//freopen("input.txt","r",stdin);
  	int T;
  	cin>>T;
  	for(int t=1; t<=T; t++)
	 {
   		cin>>m>>n;
   		cin>>k;
   		flag=1;
   		minnum=999999;
   		for(int i=1; i<=m; i++)
		{
    		for(int j=1; j<=n; j++) 
			{
     			cin>>map[i][j];
    		}
   		}
 	if(m==1&&n==1) cout<<"0"<<endl;
    else bfs();
  	}
  return 0;
 } 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值