URAL 1298 knight dfs搜索

1298. Knight

Time limit: 2.0 second

Memory limit: 64 MB

Even paratroopers have vacations. The flight to Sirius in the depths of “The Admiral Brisco” Leo Hao whiled away with chessboard. No, he did not like usual chess game, and in addition, he did not have
likely rival. The whole day Leo amused himself with an interesting thing: he tried to travel over all cells of the chessboard with the knight so that the knight visited each cell only one time. Leo attempted one time, then second, but always something was
wrong. Leo became a little angry. Then he attempted board 4*4 instead of 8*8. Again failure after failure. A little angry, with the chessboard under his arm, Leo went to look for a local programmer. They two together indeed will solve this problem.

Input

There is only one number N (1 ≤ N ≤ 8) in the input.

Output

If it is possible to travel with the knight over the square field N×N cells, then output should contain N2 lines
with tour over the chessboard with mentioned property, otherwise the only word “IMPOSSIBLE”.

Samples

input output
3
IMPOSSIBLE
5
a1
c2
e1
d3
e5
c4
d2
e4
c5
a4
b2
d1
e3
d5
b4
a2
c1
e2
c3
b1
a3
b5
d4
b3
a5

题意:有一个n*n的棋盘。从任意一个位置开始走。走法和象棋中的马一样。要求走过的点不能再走。输出可以走完所有点的走法。

否者输出IMPOSSIBLE。

做法:用dfs搜索所有的点。。。  并记录走的路线

不知道为什么dir的方向如果不一样,得到的 时间 差的很远。

我用的两种dir 一个只用609ms,一个用了1900ms的时间。

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

//int dir[8][2]={-1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1, 1,2}; //这个dir用1900ms的时间

int dir[8][2]={2,1,  1,2  ,-1,2   ,-2,1  ,-2,-1   ,-1,-2  ,  1,-2,  2,-1};// 这个dir 用609ms的时间
int n;
int lim;
char rrr[70];
int ccc[70];
int vis[10][10];
int flag;
int ok(int x,int y)
{
	if(x>=1&&x<=n&&y>=1&&y<=n&&vis[x][y]==0)
		return 1;
	return 0;
} 

int dfs(int x,int y,int num)
{
	if(n*n==num)
		return flag=num;
	for(int i=0;i<8;i++)
	{
		int xx=dir[i][0]+x;
		int yy=dir[i][1]+y;
		if(ok(xx,yy))
		{
			rrr[lim]=xx+'a'-1;
			ccc[lim++]=yy;
			vis[xx][yy]=1;
			dfs(xx,yy,num+1);
			if(flag)
				return flag;
			vis[xx][yy]=0;
			lim--;

		}
	}
	return 0;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	//for(n=1;n<=8;n++)
	{
		memset(vis,0,sizeof vis);
		lim=0;
		flag=0;
		vis[1][1]=1;

		rrr[lim]='a';
		ccc[lim++]=1;
		dfs(1,1,1); 

		if(flag)
		{
			for(int i=0;i<lim;i++)
				printf("%c%d\n",rrr[i],ccc[i]);
		}
		else
			puts("IMPOSSIBLE");

	}
	return 0;
}
时间: 2024-08-08 09:38:54

URAL 1298 knight dfs搜索的相关文章

URAL 1298. Knight(DFS啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1298 1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard.

hdu1372 dfs搜索之国际象棋的马

原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到另一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四个方向.而是由"日" 字组成的8个方向.虽然是国际象棋的马,但是其实和中国象棋的马走法还是一样的. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std;

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

nyist oj 19 擅长排列的小明(dfs搜索+STL)

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

【计算几何】URAL - 2101 - Knight&#39;s Shield

Little Peter Ivanov likes to play knights. Or musketeers. Or samurai. It depends on his mood. For parents, it is still always looks like "he again found a stick and peels the trees." They cannot understand that it is a sword. Or epee. Or katana.

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu1455 dfs搜索之凑棍子

原题地址 这道题和poj的拯救少林神棍是一样的题目. 要用给出的小棍凑成等长的棍子,求能凑成的棍子的最小长度. 直观的包里思路就是枚举所有可能的长度,然后不停的测试小棍组合,先把小棍加入组合,然后不合适就推翻这一根小棍,再测试下一个小棍,直到推翻所有的小棍. 在枚举的时候,我们只需从最长的小棍长,枚举到小棍总长的一半就行了.然后如果再不符合的话,那么就说明所有小棍只能组合成一根棍子了. 我原先看过关于poj上拯救少林神棍这道题目的详细讲解.一个DFS搜索题,这里DFS共有四种剪枝方案: 所给出的

nyist oj 58 最少步数(dfs搜索)

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为