TOJ_12470
#include <stdio.h>
struct node{
int x;
int y;
int step;
}first;
int zx[4]={-1,0,1,0};
int zy[4]={0,-1,0,1};
int a[10][10]={0};
int g[10];
int f=0;
node queue[100]={0};
int head=0,tail=0;
int flagx,flagy,flag1x,flag1y;
//进栈
void en(node E)
{
queue[tail++]=E;
}
//出栈
node qe()
{
return queue[head++];
}
void bfs()
{
int b[10][10]={0};
node cur={};
node ns={};
first.x=flagx;
first.y=flagy;
en(first);
b[first.x][first.y]=1;
int d1=1,d2=0,d3=0,d4=0;
printf("%d %d %d\n",first.x ,first.y,first.step);
while(head<tail)
{
cur=qe();
printf("%d %d\n",cur.x ,cur.y); //已经数字入队了
if(cur.x==flag1x&&cur.y==flag1y)
{
g[f++]=cur.step;
break;
}
for(int i=0;i<4;i++)
{
ns=cur;
ns.x=ns.x+zx[i];
ns.y=ns.y+zy[i];
printf("%d %d\n",ns.x,ns.y);
printf("%d %d\n",cur.x,cur.y);
//直走
if(ns.x-cur.x==-1)
{
if(a[ns.x][ns.y]=='.'||a[ns.x][ns.y]=='T')
{
if(!b[ns.x][ns.y])
{
b[ns.x][ns.y]=1;
d1++;
d2=0;
d3=0;
d4=0;
if(d1==1)
ns.step++;
ns.step++;
//printf("step=%d\n",ns.step);
en(ns);
}
}
}
//后退
if(ns.x-cur.x==1)
{
if(a[ns.x][ns.y]=='.'||a[ns.x][ns.y]=='T')
{
if(!b[ns.x][ns.y])
{
b[ns.x][ns.y]=1;
d1=0;
d2=0;
d3=0;
d4++;
if(d4==1)
ns.step++;
ns.step++;
//printf("step=%d\n",ns.step);
en(ns);
}
}
}
//左转
if(ns.y-cur.y==-1)
{
if(a[ns.x][ns.y]=='.'||a[ns.x][ns.y]=='T')
{
if(!b[ns.x][ns.y])
{
b[ns.x][ns.y]=1;
d1=0;
d2++;
d3=0;
d4=0;
if(d2==1)
ns.step++;
ns.step++;
//printf("step=%d\n",ns.step);
en(ns);
}
}
}
//右转
if(ns.y-cur.y==1)
{
if(a[ns.x][ns.y]=='.'||a[ns.x][ns.y]=='T')
{
if(!b[ns.x][ns.y])
{
b[ns.x][ns.y]=1;
d1=0;
d2=0;
d3++;
d4=0;
if(d3==1)
ns.step++;
ns.step++;
//printf("step=%d\n",ns.step);
en(ns);
}
}
}
}
}
if(cur.x!=flag1x&&cur.y!=flag1y)
{
g[f++]=-1;
}
}
void main()
{
int t,w,l;
scanf("%d",&t);
for(int j=0;j<t;j++)
{
scanf("%d %d",&w,&l);
for(int i=0;i<w;i++)
{
getchar();
for(int j=0;j<l;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S')
{
flagx=i;
flagy=j;
a[i][j]='#';
}
if(a[i][j]=='T')
{
flag1x=i;
flag1y=j;
}
}
}
first.step=0;
bfs();
}
for(int i=0;i<f;i++)
printf("%d\n",g[i]);
}
TOJ_12470的更多相关文章
随机推荐
- sobel 使用说明
转自http://www.cnblogs.com/justany/archive/2012/11/23/2782660.html OpenCV 2.4+ C++ 边缘梯度计算 2012-11-23 0 ...
- 面向对象 Java练习
package xin.bao; public class Pingguo { private String Zhonglei;// 种类 public String getZhonglei() { ...
- centos7 编译安装mysql
centos 7 安装mySql 1,准备mySql源码安装 #wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar ...
- org.json
org.json很经典.能解析json和序列化List.Map为json,但是不能自动填充bean.不依赖其它架包. 直接上代码: import java.util.ArrayList; import ...
- Python 函数式编程和OOP编程 0001测试
# encoding: utf- stu1 = { ' } stu2 = { ' } def stu_score(stu_score): print ('%s , %s ' % ( stu_score ...
- Picard Tools
Picard Tools - By Broad Institute http://broadinstitute.github.io/picard/command-line-overview.html ...
- js把一个数组插入到另一个数组的指定位置
var arr1 = ['a', 'b', 'c']; var arr2 = ['1', '2', '3']; // 把arr2 变成一个适合splice的数组(包含splice前2个参数的数组) a ...
- 二进制搭建kubernetes多master集群【二、配置flannel网络】
上一篇我们已经搭建etcd高可用集群,参考:二进制搭建kubernetes多master集群[一.使用TLS证书搭建etcd集群] 此文将搭建flannel网络,目的使跨主机的docker能够互相通信 ...
- yii框架场景的用法
1.在 model 里面定义一下场景 类名必须是 scenarios() public function scenarios() { return [ 'create' => ['title', ...
- EF生成的SQL语句执行顺序问题。
//实体被更改后,再做删除,EF只生成删除语句 //实体删除后再更改,EF报错 //添加语句会再,更改,删除后执行,更AddObject位置无关 //一个实体多个字段被改,只会生成一句update / ...