Diamond

Diamond

时间: 1ms        内存:32M

描述:

Diamond mine is a mini-game which is played on an 8 * 8 board as you can see below.
The board is filled with different colors of diamonds. The player can make one move at a time. A move is
legal if it swaps two adjacent diamonds(not diagonally) and after that, there are three or more adjacent
diamonds in a row or column with the same color. Those diamonds will be taken away and new
diamonds will be put in their positions. The game continues until no legal moves exist.
Given the board description. You are going to determine whether a move is legal.

输入:

The input contains several cases. Each case has exactly 9 lines. The first 8 lines each contains a string
of 8 characters. The characters are 'R'(Red), 'O'(Orange), 'G'(Green), 'P'(Purple), 'W'(White), 'Y'(Yellow)
or 'B'(Blue). All characters are uppercase. No 3 diamonds of the same color are initially in adjacent
positions in a row or column. The last line has 4 integers in the form " row1 column1 row2 column2"
describing the postions of the 2 diamonds that the player wants to swap. Rows are marked 1 to 8
increasingly from top to bottom while columns from left to right. Input is terminated by EOF.

输出:

For each case, output "Ok!" if the move is legal or "Illegal move!" if it is not.

示例输入:

PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
4 3 3 3
PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
5 5 6 5

示例输出:

Ok!
Illegal move!

提示:

参考答案(内存最优[752]):

#include <stdio.h>
#include <math.h>
char map[8][9];
int ok(int i,int j,char ch)
{
	return i>=0&&j>=0&&i<8&&j<8&&ch==map[i][j];
}
int ok2(int i,int j)
{
		return i>=0&&j>=0&&i<8&&j<8;
}
int can(int si,int sj,int ei,int ej)
{
	return ok2(si,sj)&&ok2(ei,ej)&&( (si==ei&&fabs(sj-ej)==1) || (sj==ej&&fabs(si-ei)==1));
}
int amount(int si,int sj)
{
	int i,count=1;

	for(i=1;;i++)
		if( ok(si-i,sj,map[si][sj]) ) count++;
		else break;
	for(i=1;;i++)
		if( ok(si+i,sj,map[si][sj]) ) count++;
		else break;
	if(count>2) return 1;
	for(i=count=1;;i++)
		if( ok(si,sj-i,map[si][sj]) ) count++;
		else break;
	for(i=1;;i++)
		if( ok(si,sj+i,map[si][sj]) ) count++;
		else break;
	if(count>2) return 1;
	else return 0;
}
int main()
{
	int i,si,sj,ei,ej;
	char t;

	while(scanf("%s",map[0])!=EOF)
	{
		for(i=1;i<8;i++)
			scanf("%s",map[i]);
		scanf("%d%d%d%d",&si,&sj,&ei,&ej);
		si--;sj--;ei--;ej--;
		t=map[si][sj];
		map[si][sj]=map[ei][ej];
		map[ei][ej]=t;
		if( can(si,sj,ei,ej) && (amount(si,sj) || amount(ei,ej))) puts("Ok!");
		else puts("Illegal move!");
	}
	return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <vector>
#include <string>
using namespace std;

int main()
{
	int i;
	char s[101],op[3],s2[101];
	
	while(scanf("%s",s),s[0]!='.')
	{
		scanf("%s%s",op,s2);
		vector<string> m;
		vector<string>::iterator it;
		string t;
		for(i=0;s[i];i++)
		{
			if(s[i]!='[')
			{
				if(s[i]==','||s[i]==']')
				{
					if( t!="" ) m.push_back(t);
					t="";
				}
				else t+=s[i];
			}
			
		}
		for(i=0;s2[i];i++)
		{
			if(s2[i]!='[')
			{
				if(s2[i]==','||s2[i]==']')
				{
					if(op[0]=='+')
					{
						if( t!="" ) m.push_back(t);
					}
					else
					{
						for(it=m.begin();it!=m.end();it++)
						{
							if( (*it)==t )
							{
								m.erase(it);
								break;
							}
						}
					}
					t="";
				}
				else t+=s2[i];
			}
		}
		putchar('[');
		for(i=0;i<m.size();i++)
		{
			printf("%s%s",m[i].c_str(),i==m.size()-1?"":",");
		}
		putchar(']');
		putchar(10);
	}
	return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注