首页 > 搜索 > BFS搜索 > HDU 3355-Hop ― Don’t Walk!-BFS-[解题报告]HOJ
2014
03-16

HDU 3355-Hop ― Don’t Walk!-BFS-[解题报告]HOJ

Hop ― Don’t Walk!

问题描述 :

KERMIT THE FROG is a classic video game with a simple control and objective but requires a good deal of thinking.
You control an animated frog that can walk and hop, in both forward and backward directions. The frog stands in a space between an otherwise a contiguous line of tiles. Each tile is painted black on one side, and white on the other. The frog can walk (forward, or backward) over an adjacent tile (in front or behind him.)
When the frog walks over a tile, the tile slides to the space where the frog was standing. For example, in the adjacent figure, the frog has two tiles behind him, and three in front. We’ll use the notation BWFBBW to refer to this situation where F refers to the space (where the frog is standing,) B is a tile with its black face showing, while W is a tile with its white face showing. The forward direction is from left to right. If the frog were to walk forward, the resulting situation is BWBFBW. Similar behavior when the frog walks backward, the tile behind the frog slides to where the frog was standing. The frog can also hop over the tiles. The frog can hop over an adjacent tile landing on the tile next to it. For example, if the frog was to hop backward, it would land on the first (left-most) tile, and the tile would jump to the space where the frog was standing. In addition, the tile would flip sides. For example, hopping backward in the figure would result in the situation: FWWBBW. We challenge you to write a program to determine the minimum number of moves (walks or hops) to transform one tile configuration into another.

Probability One

输入:

Your program will be tested on one or more test cases. Each test case is specified on a single line that specifies string S representing the initial tile arrangement. S is a non-empty string and no longer than 100 characters and is made of the letters ’B’, ’W’, and exactly one ’F’. The last line of the input file has one or more ’-’ (minus) characters.

输出:

Your program will be tested on one or more test cases. Each test case is specified on a single line that specifies string S representing the initial tile arrangement. S is a non-empty string and no longer than 100 characters and is made of the letters ’B’, ’W’, and exactly one ’F’. The last line of the input file has one or more ’-’ (minus) characters.

样例输入:

WWBBFBW
WWFBWBW
FWBBWBW
---

样例输出:

1. 0
2. 1
3. 2

#include<cstdio>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;

int c,ans;
struct node
{char s[105];int step,p;}s;
bool ok(char *s)
{
	int i,l=0,r=0;
	for(i=0;s[i];i++)
		if(s[i]=='B')
		{
			l=i;
			break;
		}
	for(i=strlen(s)-1;i>=0;i--)
		if(s[i]=='B')
		{
			r=i;
			break;
		}	
	for(i=l+1;i<r;i++)				
		if(s[i]=='W')
			return 0;
	return 1;
}
void bfs()
{
	map<string,int> mm;
	queue<node> q;
	q.push(s);
	mm[s.s]=1;
	int p;
	while(q.size())
	{		
		node ct=q.front();
		q.pop();
		p=ct.p;
		//printf("%d %d %s/n",ct.step,ct.p,ct.s);
		if(ok(ct.s))
		{
			ans=ct.step;
			return;
		}
		if(ct.step<10)
		{
			if(ct.p)
			{
				node ad=ct;
				ad.step++;
				swap(ad.s[p],ad.s[p-1]);
				ad.p--;
				if(!mm[ad.s])
				{
					q.push(ad);
					mm[ad.s]=1;
				}
			}
			if(ct.p+1<strlen(ct.s))
			{
				node ad=ct;
				ad.step++;
				swap(ad.s[p],ad.s[p+1]);
				ad.p++;
				if(!mm[ad.s])
				{
					q.push(ad);
					mm[ad.s]=1;
				}
			}
			if(ct.p>1)
			{
				node ad=ct;
				ad.step++;
				swap(ad.s[p],ad.s[p-2]);
				if(ad.s[p]=='W')
					ad.s[p]='B';
				else
					ad.s[p]='W';
				ad.p-=2;
				if(!mm[ad.s])
				{
					q.push(ad);
					mm[ad.s]=1;
				}
			}
			if(ct.p+1<strlen(ct.s))
			{
				node ad=ct;
				ad.step++;
				swap(ad.s[p],ad.s[p+2]);
				if(ad.s[p]=='W')
					ad.s[p]='B';
				else
					ad.s[p]='W';
				ad.p+=2;
				if(!mm[ad.s])
				{
					q.push(ad);
					mm[ad.s]=1;
				}
			}
		}
	}
}
int main()
{
	//freopen("hop.in","r",stdin);
	//freopen("1.out","w",stdout);
	while(scanf("%s",s.s),s.s[0]!='-')
	{
		int i,p;
		ans=10;
		if(ok(s.s))
			printf("%d. %d/n",++c,0);
		else
		{
			for(i=0;s.s[i];i++)
				if(s.s[i]=='F')
					p=i;
			s.step=0;
			s.p=p;
			bfs();
			printf("%d. %d/n",++c,ans<10?ans:-1);
		}		
	}
}

参考:http://blog.csdn.net/power721/article/details/5816273


,
  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }