码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3050 Hopscotch (穷竭搜索)

时间:2015-08-09 12:46:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:搜索   poj   穷竭搜索   

题目链接:http://poj.org/problem?id=3050


题面:

Hopscotch
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2563   Accepted: 1823

Description

The cows play the child‘s game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes.

They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited).

With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201).

Determine the count of the number of distinct integers that can be created in this manner.

Input

* Lines 1..5: The grid, five integers per line

Output

* Line 1: The number of distinct integers that can be constructed

Sample Input

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1

Sample Output

15

Hint

OUTPUT DETAILS:
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.

Source


题目大意:

    一只牛在5*5的方格内可以从任意点开始跳,每次可以往4个方向跳,共跳5次,根据走过的方格,形成一个长度为6的序列,问这样的序列有多少种?(方格可以重复跳。)


解题:

    题目挂的时候写的是穷竭搜索,看到图这么小,估计就是暴搜的意思吧。枚举从每个点开始跳,跳到5步后停止,并记录此时形成的路径,用set排重计数即可。


代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <vector>
using namespace std;
char map[6][6],path[10];
//四个方向
int dir[4][2]={0,1,-1,0,0,-1,1,0};
//计数
set <string> cnt;
string tmp;
//判断是否方格内
bool inside(int x,int y)
{
	if(x>=1&&x<=5&&y>=1&&y<=5)
		return true;
	else 
		return false;
}
//深搜
void dfs(int x,int y,int step)
{
	int tx,ty;
	path[step]=map[x][y];
	if(step==5)
	{
		//记录路径
	   tmp="";
       for(int i=0;i<=5;i++)
		   tmp+=path[i];
	   cnt.insert(tmp);
	   return;
	}
	//往四个方向跳
	for(int i=0;i<4;i++)
	{
		tx=x+dir[i][0];
		ty=y+dir[i][1];
		if(inside(tx,ty))
			dfs(tx,ty,step+1);
	}
}
int main()
{
	//读入
    for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			scanf(" %c",&map[i][j]);
	//枚举从每个点开始
	for(int i=1;i<=5;i++)
		for(int j=1;j<=5;j++)
			dfs(i,j,0);
	//输出
	printf("%d\n",cnt.size());
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3050 Hopscotch (穷竭搜索)

标签:搜索   poj   穷竭搜索   

原文地址:http://blog.csdn.net/david_jett/article/details/47373689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!