`

Rotate problem

阅读更多

      这是2010年Google Code Jam里的一道问题,我将原题的大概意思翻译成中文了,附上自己的解决过程。

 

Problem:

      在一个NXN的点阵中下棋,棋盘是直立的,所有棋子都会从上往下落,不会悬空在某一位置,棋子分为红色‘R’和蓝色‘B'两种,如下图,其中左边为正确的棋盘,右边为错误的棋盘,在右边的图中,棋子B应该要往下落一格:


                        

      棋盘是可以旋转的,每次旋转90度,可以逆时针旋转,也可以顺时针旋转,但是旋转后棋盘中的棋子之间的相对位置就会发生变化,如下图,旋转之后所有的棋子会由于重力的影响在棋盘中的相对位置都发生了变化:


                     

       下棋胜利的评判标准就是有连续K个相同棋子相连,这和五子棋的判别是一样的。关键是旋转的问题,因为旋转后棋子的位置会变。

       约束条件:

       1、只能旋转1次;

       2、重力作用只在棋盘完全旋转90度之后才有效果;
       3、如果需要旋转棋盘,必须完全旋转后,且棋子全部下落后才能进行局面评估。
      问题输入的文件格式和输出格式如下图,输入格式中,文件的第一行是测试用例的个数,从第二行开始就是第一个用例的开始,第一个用例的第一行有两个数,第一个为7,代表是7X7的棋盘,第二个数3代表连续3个相连就算胜利。输出文件中,Neither表示红与黑都没有K个棋子相连,Red/Blue表示只有红色或者只有蓝色满足条件,Both表示都可以。


                                                    

 

解决方法:

       主要面对的问题就是旋转的次数只能是一次,如果不旋转又难以判定两种棋子是否符合条件。这个问题的解决办法就是无需旋转。因为可以发现,旋转后的棋子效果相当于将重力的方向改变90度即可,比如若将棋盘逆时针旋转90度达到的效果和将所有棋子推到左边所达到的效果是一样的。这样就完全不会用到旋转就可以判定两种棋子是否符合条件了。

       向左移动棋子的代码:

for( int i = 0; i < N; i++ )
	{
		int x = 0;
		for( int j = 0; j < N; j++ )
		{
			if( piece[i][j] != '.' )
			{
				piece[i][x] = piece[i][j];
				x++;
			}
		}
		while( x<N )
		{
			piece[i][x] = '.';
			x++;
		}
	}

 

 向右移动棋子的代码:

for( int i = 0; i < N; i++ )
	{
		int x = N-1;
		for( int j = N-1; j >= 0; j-- )
		{
			if( piece[i][j] != '.' )
			{
				piece[i][x] = piece[i][j];
				x--;
			}
		}
		while( x>=0 )
		{
			piece[i][x] = '.'; 
			x--;
		}
	}

 局面评估的方法就是如果该点有棋子,就判断该棋子的8个方向,每个方向判断K步。代码如下:

//对有颜色的点的8个方向进行判断K步
/*
  1   2   3
    . . .
  8 . R . 4
    . . . 
  7   6   5
*/
bool Test_8_Director( vector<vector<char> > piece, int i, int j, char color, int N, int K )
{
	int n = 1;

	//方向1
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i-n)<0 || (j-n)<0 )   
			break;
		//没有连续K个相同
		if( piece[i-n][j-n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向2
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i-n)<0 )   
			break;
		//没有连续K个相同
		if( piece[i-n][j] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向3
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i+n)>=N || (j+n)>=N )   
			break;
		//没有连续K个相同
		if( piece[i+n][j+n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向4
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (j+n)>=N )   
			break;
		//没有连续K个相同
		if( piece[i][j+n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向5
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i+n)>=N || (j+n)>=N )   
			break;
		//没有连续K个相同
		if( piece[i+n][j+n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;
			
	//方向6
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i+n)>=N )   
			break;
		//没有连续K个相同
		if( piece[i+n][j] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向7
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (i+n)>=N || (j-n)<0 )   
			break;
		//没有连续K个相同
		if( piece[i+n][j-n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	//方向8
	n = 1;
	while( n <= K )
	{
		//没达到K步就到边上了
		if( (j-n)<0 )   
			break;
		//没有连续K个相同
		if( piece[i][j-n] != color )
			break;
		n++;
	}
	if( n >= K )
		return true;

	return false;
}

 


 

 

  • 大小: 2 KB
  • 大小: 2.2 KB
  • 大小: 4 KB
分享到:
评论

相关推荐

    RotatE:Knowledge Graph Embedding by Relational Rotation in Complex Space.pdf

    We study the problem of learning representations of entities and relations in knowledgegraphsforpredictingmissinglinks. Thesuccessofsuchataskheavily relies on the ability of modeling and inferring the...

    Fluent Animation – An incredible animation queue system v0.8

    It uses a procedural/functional approach to solving a decade old problem, and it's quite powerful as well: - Powerful animation queue system - Move, rotate, scale almost anything - Execute ...

    编程珠玑全部源代码 分享

    Column 13: Set representations for the problem in Column 12 sets.cpp -- Several data structures for sets. genbins.c (Column 9) implements the bin data structure in C. Column 14: Heaps priqueue....

    iphone h.264 live encode 实时 硬编码

    My example code takes the following approach to this problem: Only video is written using the AVAssetWriter instance, or it would be impossible to distinguish video from audio in the mdat atom. ...

    Pathfinder:使用具有16个字符的十六进制系统进行通信

    探路者In "The Martian," astronaut Mark Watney is stranded on Mars. In order to contact with NASA, Watney digs up ... All they have is a camera on a platform which can rotate 360 degrees.That's a start.

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    STG (SNMP Traffic Grapher)

    Rotate N Log Files - specifies number of log files to rotate - minimum 1. Rotated files will have numeric extention added: *.000 *.001 etc. Every N Hour(s) or Day(s) or Week(s) or Month(s) - ...

    TSP的量子进化算法的新量子旋转角

    本文对量子旋转门进行了改进,这是传统量子进化算法在种群更新中的主要操作。 定义了新的旋转角度,以防止算法在中期和后期容易陷入局部最佳状态。 根据TSP的特点,提出了一种改进的量子旋转门,根据进化代数和对...

    VB编程资源大全(英文源码 其它)

    cube.zip This example demonstrates how to rotate a cube in visual basic.&lt;END&gt;&lt;br&gt;17 , sprite1.zip This is an Excellent example on how to use sprites in your program.&lt;END&gt;&lt;br&gt;18 , charcreate.zip...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The PointOnLine() function calulations have "single" type numbers overflow problem (changed to "double"). - FIX: The pfJoin and pfClose flags incorrectly calculates in GetEditPathCaps(). ...

    postGIS 用户手册

    6.1.1 Problem description . . . . . . . . . . . . . . . 49 6.1.2 Workarounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.2 CLUSTERing on geometry indices . . . . . . . . . . . . . ....

Global site tag (gtag.js) - Google Analytics