POJ 3050 Hopscotch (穷竭搜索)

题目链接: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

USACO 2005 November Bronze

题目大意:

一只牛在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;
}

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

时间: 2024-08-06 06:19:12

POJ 3050 Hopscotch (穷竭搜索)的相关文章

POJ 2718 Smallest Difference (穷竭搜索)

http://poj.org/problem?id=2718 将一个数切一刀拆成两个数,两个数每一位数字的顺序都可改变,但是不能有前导0.求这两个数之差的最小值. 我使用了搜索并且避免了递归,自认为是比较好的算法. 一开始我想到的是枚举,并且很快给出了实现: #ifndef ONLINE_JUDGE #pragma warning(disable : 4996) #endif #include <iostream> #include <string> #include <al

挑战程序2.1.4 穷竭搜索&gt;&gt;深度优先搜索

{'W','.','.','W','.'}, {'W','.','.','.','.'}, {'.','.','.','W','.'}, {'W','.','.','.','.'}, {'W','.','W','W','W'}例子,此处有5处积水 题目:有N*M格子的花园,W表示积水,'.'表示干地,积水附近8个区域只要有积水,那么这两处连通,并算作是同一处积水,问一共有几处积水? 思路:穷竭搜索,O(N*M),搜索到积水则变成'.',并搜索连通的积水将其变成'.',直到这块积水搜索结束,此时a

挑战程序设计竞赛 2.1 最基础的“穷竭搜索”

[Summarize] 1.划分为两堆的无序模型可以利用二进制枚举, 而划分为两堆的有序模型可以枚举全排列取定长 2.当搜索终态唯一时可考虑逆向搜索 POJ 1979:Red and Black /* 一个矩形的屋子里有一个人,他可以踩黑格子,但是不能踩红格子, 他现在站在一个黑格子上,告诉你房间的布局, 这个人可以向上下左右四个方向移动,问这个人最多能到达的格子数 */ #include <cstdio> #include <cstring> using namespace st

POJ-3050 Hopscotch(穷竭搜索,DFS,回溯法)

http://poj.org/problem?id=3050 Calm Down FIRST 题意:给定一个5*5的地图,每个格子上有一个数字.从一个格子出发(上下左右4个方向),走5步将数字连起来可以构造出一个6位数.问该地图可以构造出多少个不同的6位数. 分析:可以对每个格子做深度优先遍历,构造出所有数字,但要注意不要重复计数.在这里,我使用了set来保存已构造出的数字,结果就是set中的元素个数. #include <cstdio> #include <set> #inclu

poj 3050 Hopscotch【搜索、去重】

点击打开题目 Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2126   Accepted: 1524 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 cow

穷竭搜索: POJ 2718 Smallest Difference

题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一刀,比如  n1:[1 4 5],n2:[6 8 9]这样,然后 abs(n1- n2),对n1 和 n2的所有可能的排列 n1: [1 4 5][1 5 4]...这样,要算出来的最小的差,显然从中间切一刀才会出现这种解. 题解: 这里可以用来练习 STL,算法不会也没有关系,可以在这题学到 bi

poj 2718 Smallest Difference(穷竭搜索dfs)

Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

poj 3187 Backward Digit Sums(穷竭搜索dfs)

Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number