推箱子一步判断(四个方向都有)
2017-10-12 15:23
218 查看
描述:
有一个房间,房间里有一个人P,房间是规格为 n*n的方格,房子内某些位置上有箱子B,有些位置上有障碍物,人处于某一位置,可以选择向某方向前进,但要保证面对的不是墙、障碍物,若面对的是墙或障碍物则无法前进,若人面对箱子但箱子的前面为障碍物或者墙或者箱子,也不能前进,若为箱子并且箱子所面对的不是墙,不是障碍物也不是箱子,即为空格,则可以推动箱子,即人和箱子分别向这个方向移动一个格子。
输入:
房间的规格,一个正整数 n(2<=n<=100),占一行,代表矩阵的大小;输入方形矩阵n行n列,由0、1和2组成(其中0代表空地,1代表箱子,2代表障碍物),即房子的地图,输入一对数据row,col(第row行,第col列),表示人所在的位置坐标(保证人所在的位置为空地),以空格隔开,占一行;输入一个整数2,表示向右方向前进(0,1,2,3分别为向左下右上前进)。
输出:
"1"表示可以向该方向前进;
"0"表示不能向该方向前进;
输出占一行;
输入样例:
4
0 0 0 0
0 1 1 0
0 0 1 0
0 2 0 0
2 1
2
输出样例:
1
#include<iostream>
using namespace std;
int main()
{
int n,a,b,row,col,e;
cin>>n;
int A
;
for(a=0;a<n;a++)
{
for(b=0;b<n;b++)
{
cin>>A[a][b];
}
}
cin>>row>>col;
cin>>e;
if(e==0)
{
if(col-1<0)
{
cout<<"0";
}
else
{
if(A[row][col-1]==0)
{
cout<<"1";
}
else if(A[row][col-1]==2)
{
cout<<"0";
}
else
{
if(col-2<0)
{
cout<<"0";
}
else if(A[row][col-2]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else if(e==1)
{
if(row+1>n-1)
{
cout<<"0";
}
else
{
if(A[row+1][col]==0)
{
cout<<"1";
}
else if(A[row+1][col]==2)
{
cout<<"0";
}
else
{
if(row+2>n-1)
{
cout<<"0";
}
else if(A[row+2][col]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else if(e==2)
{
if(col+1>n-1)
{
cout<<"0";
}
else
{
if(A[row][col+1]==0)
{
cout<<"1";
}
else if(A[row][col+1]==2)
{
cout<<"0";
}
else
{
if(col+2>n-1)
{
cout<<"0";
}
else if(A[row][col+2]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else
{
if(col-1<0)
{
cout<<"0";
}
else
{
if(A[row-1][col]==0)
{
cout<<"1";
}
else if(A[row-1][col]==2)
{
cout<<"0";
}
else
{
if(row-2<0)
{
cout<<"0";
}
else if(A[row-2][col]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
}
有一个房间,房间里有一个人P,房间是规格为 n*n的方格,房子内某些位置上有箱子B,有些位置上有障碍物,人处于某一位置,可以选择向某方向前进,但要保证面对的不是墙、障碍物,若面对的是墙或障碍物则无法前进,若人面对箱子但箱子的前面为障碍物或者墙或者箱子,也不能前进,若为箱子并且箱子所面对的不是墙,不是障碍物也不是箱子,即为空格,则可以推动箱子,即人和箱子分别向这个方向移动一个格子。
输入:
房间的规格,一个正整数 n(2<=n<=100),占一行,代表矩阵的大小;输入方形矩阵n行n列,由0、1和2组成(其中0代表空地,1代表箱子,2代表障碍物),即房子的地图,输入一对数据row,col(第row行,第col列),表示人所在的位置坐标(保证人所在的位置为空地),以空格隔开,占一行;输入一个整数2,表示向右方向前进(0,1,2,3分别为向左下右上前进)。
输出:
"1"表示可以向该方向前进;
"0"表示不能向该方向前进;
输出占一行;
输入样例:
4
0 0 0 0
0 1 1 0
0 0 1 0
0 2 0 0
2 1
2
输出样例:
1
#include<iostream>
using namespace std;
int main()
{
int n,a,b,row,col,e;
cin>>n;
int A
;
for(a=0;a<n;a++)
{
for(b=0;b<n;b++)
{
cin>>A[a][b];
}
}
cin>>row>>col;
cin>>e;
if(e==0)
{
if(col-1<0)
{
cout<<"0";
}
else
{
if(A[row][col-1]==0)
{
cout<<"1";
}
else if(A[row][col-1]==2)
{
cout<<"0";
}
else
{
if(col-2<0)
{
cout<<"0";
}
else if(A[row][col-2]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else if(e==1)
{
if(row+1>n-1)
{
cout<<"0";
}
else
{
if(A[row+1][col]==0)
{
cout<<"1";
}
else if(A[row+1][col]==2)
{
cout<<"0";
}
else
{
if(row+2>n-1)
{
cout<<"0";
}
else if(A[row+2][col]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else if(e==2)
{
if(col+1>n-1)
{
cout<<"0";
}
else
{
if(A[row][col+1]==0)
{
cout<<"1";
}
else if(A[row][col+1]==2)
{
cout<<"0";
}
else
{
if(col+2>n-1)
{
cout<<"0";
}
else if(A[row][col+2]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
else
{
if(col-1<0)
{
cout<<"0";
}
else
{
if(A[row-1][col]==0)
{
cout<<"1";
}
else if(A[row-1][col]==2)
{
cout<<"0";
}
else
{
if(row-2<0)
{
cout<<"0";
}
else if(A[row-2][col]==0)
{
cout<<"1";
}
else
{
cout<<"0";
}
}
}
}
}
相关文章推荐
- js用斜率判断鼠标进入div的四个方向
- JS用斜率判断鼠标进入DIV四个方向的方法 判断鼠标移入方向
- 推箱子-根据keycode判断推的方向
- js用斜率判断鼠标进入div的四个方向
- JS用斜率判断鼠标进入DIV四个方向的方法
- js用斜率判断鼠标进入div的四个方向
- H5案例分享:移动端touch事件判断滑屏手势的方向
- iphone之判断屏幕方向
- 四个ping命令判断网络故障
- 【每日一C之二十一】判断栈增长方向
- 判断鼠标的移入方向
- 话里话外:装备制造业的四个转变方向
- 【转载】UIScrollerView滚动方向判断
- 判断两个键同时按下,这里是两个方向键
- 使用C语言判断栈的方向实例
- 利用Java输出九九乘法表(从四个方向排列)
- 收集整理的四个方向的滚动
- 判断鼠标滚动方向
- touchstart,touchmove判断手机中滑屏方向
- 优雅的判断scrollView的滑动方向(只适用一次手势中不改变滑动方向)