首页 > ACM题库 > HDU-杭电 > HDU 3206-Gem Squares[解题报告]HOJ
2014
03-06

HDU 3206-Gem Squares[解题报告]HOJ

Gem Squares

问题描述 :

You are given a board with 8×8 squares. In each square, there can be either a colored gem or no gem at all. Gems with different colors are represented by different integers. It is guaranteed that there are no more than two consecutive gems with the same color either in a row or in a column, and that there is not any gem above a blank square.

........
........
........
........
........
..43366.
..121556
44212335

For two neighboring squares, you can exchange the gems.


........
........
........
........
........
..43366.
..111556
44222335

If there are more than two consecutive gems with the same color in a row or in a column after exchange, these gems will be taken away simultaneously. Note that a gem could be counted both in its row and in its column; refer to the sample test cases for details.


........
........
........
........
........
..43366.
.....556
44...335

If there is no gem under a gem, the gem will fall to the square below.


........
........
........
........
........
.....66.
.....556
44433335

After all gems have fallen down to the lowest place, the procedure will be repeated. If there are more than two gems with the same color in a row or in a column, these gems will be taken away simultaneously. Then some gems will fall to the squares below, if there are no gems under those gems.


........
........
........
........
........
.....66.
.....556
.......5


........
........
........
........
........
........
.....666
.....555


........
........
........
........
........
........
........
........

The procedure will be repeated until there is no gem that can be taken away.

Given a board with 8*8 squares, you task is to determine whether all gems can be taken away by a single exchange or not.

输入:

The input consists of several test cases. Each test case will be eight lines, and each line contains eight characters. If in a square there is no gem, ‘.’ is used to identify it, otherwise an integer k is used to identify the gem’s color, 1≤k≤9.

There is a blank line between two consecutive test cases.

End of input is indicated by a line consisting of 0.

输出:

The input consists of several test cases. Each test case will be eight lines, and each line contains eight characters. If in a square there is no gem, ‘.’ is used to identify it, otherwise an integer k is used to identify the gem’s color, 1≤k≤9.

There is a blank line between two consecutive test cases.

End of input is indicated by a line consisting of 0.

样例输入:

........
........
........
........
........
..43366.
..121556
44212335

........
........
........
.2......
.2.22...
.1.11...
.2.22...
.2.22...

12121212
21212121
12121212
21212121
12121212
21212121
12121212
21212121

........
........
........
........
........
...96...
...96...
.996966.

0

样例输出:

Yes
Yes
No
Yes
Hint
You can also exchange a gem with a space in its neighbor. For test case 2, all gems can be taken away by exchanging the leftmost “1” and the space on the right.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
char maz[10][10];
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
bool check(int x,int y)
{
 return x>=1&&x<=8&&y>=1&&y<=8;
}
char tmp[10][10];
void down()
{
 int p=8;
 for(int i=1;i<=8;i++)
 {
 p=8;
 while(tmp[p][i]!='.')
 p--;
 int n=p;
 while(p>=1)
 {
 if(tmp[p][i]!='.')
 {
 tmp[n][i]=tmp[p][i];
 tmp[p][i]='.';
 n--;
 }
 p--;
 }
 }
}
int delt()
{
 int cnt[4];
 int amount=0;
 int tx,ty;
 bool visited[10][10];
 memset(visited,0,sizeof visited);
 for(int i=8;i>=1;i--)
 {
 for(int j=1;j<=8;j++)
 {
 cnt[0]=cnt[1]=cnt[2]=cnt[3]=0;
 if(tmp[i][j]=='.'||visited[i][j]) continue;
 for(int k=0;k<4;k++)
 {
 //cnt[k]=0;
 int tx=i+dirx[k];
 int ty=j+diry[k];
 while(tmp[tx][ty]==tmp[i][j])
 {
 cnt[k]++;
 tx=tx+dirx[k];
 ty=ty+diry[k];
 }
 }
 for(int m=0;m<2;m++)
 {
 if(cnt[m]+cnt[m+2]+1>=3)
 {
 tx=i+cnt[m]*dirx[m];ty=j+cnt[m]*diry[m];
 for(int k=0;k<cnt[m]+cnt[m+2]+1;k++)
 {
 visited[tx][ty]=1;
 tx+=dirx[m+2];
 ty+=diry[m+2];
 }
 }
 }
 }
 }
 for(int i=1;i<=8;i++)
 for(int j=1;j<=8;j++)
 if(visited[i][j])
 {
 tmp[i][j]='.';
 amount++;
 }
 if(amount) down();
 return amount;
}
void outp()
{
 for(int i=1;i<=8;i++)
 {
 for(int j=1;j<=8;j++)
 cout<<tmp[i][j];
 cout<<endl;
 }

}
int main()
{
	#ifdef Effca
	freopen("1.in","r",stdin);
	freopen("2.out","w",stdout);
	#endif

 while(scanf("%s",maz[1]+1))
 {
 if(maz[1][1]=='0') break;
 int all=0;
 for(int i=2;i<=8;i++)
 scanf("%s",maz[i]+1);
 for(int i=1;i<=8;i++)
 for(int j=1;j<=8;j++)
 if(maz[i][j]!='.') all++;
 int cnt,pre;
 bool flag=0;
 for(int i=8;i>=1;i--)
 {
 if(flag) break;
 for(int j=1;j<=8;j++)
 {
 if(flag) break;
 for(int k=0;k<4;k++)
 {
 if(flag) break;
 memcpy(tmp,maz,sizeof maz);
 int tx=i+dirx[k];
 int ty=j+diry[k];
 cnt=0;pre=-1;
 if(check(tx,ty)&&maz[i][j]!=maz[tx][ty])
 {
 swap(tmp[i][j],tmp[tx][ty]);
 while(cnt!=pre&&cnt!=all)
 {
 pre=cnt;
 cnt+=delt();

 }
 if(cnt==all) flag=1;
 }
 }
 }
 }
 if(flag)
 puts("Yes");
 else
 puts("No");
 }

	return 0;
}

  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  2. for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
    dp = dp [j-1] + 1;
    if(s1.charAt(i-1) == s3.charAt(i+j-1))
    dp = dp[i-1] + 1;
    if(s2.charAt(j-1) == s3.charAt(i+j-1))
    dp = Math.max(dp [j - 1] + 1, dp );
    }
    }
    这里的代码似乎有点问题? dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false