Time Limit: 1000 MS Memory Limit: 32768 KB
Total Submission(s): 920 Accepted Submission(s): 378
Description
无聊的陶陶准备编写一款游戏,名字就叫做“马踏飞燕”,在这款游戏中有个一个100*100的坐标,把马放在任意一个坐标点,再把燕子放在任意一个坐标点,并且燕子不会移动,马只能按照象棋规则走“日”。若4步之内能“踏”到燕子,则成功。笨蛋的陶陶不知道该怎么去写,现在请你帮助他。
走“日”说明:当马的坐标为(5,5)的时候,马下一步可以走的坐标有8个点,分别为(4,3)(6,3)(3,4)(7,4)(3,6)(7,6)(4,7)(6,7)
Input
第一行两个整数,马的起始坐标x,y (0<x,y<100)
第一行两个整数,燕子的坐标m,n (0<m,n<100)
Output
若4步之内能“踏”到燕子,则输出“Y”
若4步之内不能“踏”到燕子,则输出“N”
Sample Input
5 5 7 4
Sample Output
Y
Source
key:用的dfs
#include <bits/stdc++.h>
using namespace std ;
const int N = 2008 ;
const int dx[8] = {-2 , -2 ,-1 , -1 , 1 , 1 , 2 , 2} ;
const int dy[8] = {-1 , 1 , -2 , 2 , -2 , 2 , 1 ,-1} ;
int sx=0 , sy=0 , ex=0 , ey=0 ;
bool flag = false ;
bool vis[N][N] ;
bool judge(int x , int y)//不能出去了
{
if(x < 1 || x > 2000 || y < 1 || y > 2000)
return false ;
return true ;
}
void dfs(int x , int y, int cnt)//全排列
{
if(cnt > 200)
return ;
if(x == ex && y == ey)
{
flag = true ;
return ;
}
for(int i = 0 ; i < 8 ; i ++)
{
int tx = x + dx[i] ;
int ty = y + dy[i] ;
if(judge(tx , ty) && !vis[tx][ty])//1.判断为真;2.不是vis ,没有标记过
{
vis[tx][ty] = true ; //开了
dfs(tx , ty , cnt + 1) ; //递归设置
vis[tx][ty] = false ; //再关
}
}
}
int main()
{
cin >> sx >> sy ;
cin >> ex >> ey ;
dfs(sx , sy , 0) ;
if(flag)
puts("Y") ;
else
puts("N") ;
return 0 ;
}