HUST 1624 Beautiful Sky(判断两图形是否相同)
2013-12-18 12:21
447 查看
Beautiful Sky
Time Limit: 1 Sec Memory Limit: 128 MBSubmissions: 8 Solved: 3
Description
The 8th(2013) ACM Programming Contest of HUST Problem SetLittle Mine is a naughty boy. He was once an excellent student and has won a great many prizes since the primary school. However, he’s also addicted to video games. After he goes to college, when it
comes to physics, his performance turns out the opposite way.
His teacher Mr. Wang is a nice person. When hearing about Littlemine’s story, he decided to offer him an “opportunity”. If he could solve the difficult problem, he wouldn’t have to hand in his homework
for the whole semester(ACTUALLY Mr.Wang WANTS HIM TO FIND A WAY BACK TO BE A GOOD STUDENT). Here is the problem:
This is a map consisting of multiple constellations(A constellation means a union of connected stars, each one of them has at least one star in its eight adjacent positions, and one constellation cannot
be a part of another bigger constellation). Two constellations are similar if and only if they have same amount of stars and exact the same shape, ignoring the difference of their directions.
Here we use a n*m (0<=n,m<=100) matrix consisting of ones and zeros to denote the map. One means there’s a star in its position (the total number of stars won’t exceed 500), while zero means nothing.
Given such a matrix, you need to use a lower-case letter to denote all the constellations. Similar constellations must be marked with the same letter. There are at most 26 different constellations and each one has at most 160 stars. Check the example below
to learn more details.
But Little Mine doesn’t buy it, he makes up his mind to put things “right” once and for all. So he asks you for help. Will you help him?
Input
The first line is an integer T ( T <= 5) meaning there are T test cases. For each test case, the first two lines have two numbers, m and n, which represents the width and the height of the map. The next n lines describe the map, each line has exact m characters.Output
For each test case, output the related marked map. If there are multiple solutions, output the one with minimum lexicographic order. There is at least one blank line between each two cases.Sample Input
1 23 15 10001000000000010000000 01111100011111000101101 01000000010001000111111 00000000010101000101111 00000111010001000000000 00001001011111000000000 10000001000000000000000 00101000000111110010000 00001000000100010011111 00000001110101010100010 00000100110100010000000 00010001110111110000000 00100001110000000100000 00001000100001000100101 00000001110001000111000
Sample Output
a000a0000000000b0000000 0aaaaa000ccccc000d0dd0d 0a0000000c000c000dddddd 000000000c0b0c000d0dddd 00000eee0c000c000000000 0000e00e0ccccc000000000 b000000e000000000000000 00b0f000000ccccc00a0000 0000f000000c000c00aaaaa 0000000ddd0c0b0c0a000a0 00000b00dd0c000c0000000 000g000ddd0ccccc0000000 00g0000ddd0000000e00000 0000b000d0000f000e00e0b 0000000ddd000f000eee000
HINT
Source
The 8th(2013) ACM Programming Contest of HUST这个题目其实是比较简单的,可是开始没看清题意,以为镜面对称的不算
总体这样,从某点开始搜索,如果搜索路线中没发现不同就是相同,镜面
对称将原来的图回文下按照上面思路判断
搜索的时候旋转90 180 270 0,四个方向。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn = 150;
char map[maxn][maxn],map1[maxn][maxn];
int is_visit[maxn][maxn],global;
int n,m;
int dir[][2]={{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1}};
bool flag;
int is_same(int i1,int j1,int i2,int j2,int offset,char ch){
if(flag) return 0;
is_visit[i1][j1]=global;
for(int i=0;i<8;i++){
i1+=dir[i][0];
j1+=dir[i][1];
i2+=dir[(i+offset)%8][0];
j2+=dir[(i+offset)%8][1];
if(is_visit[i1][j1]==global){
i1-=dir[i][0];
j1-=dir[i][1];
i2-=dir[(i+offset)%8][0];
j2-=dir[(i+offset)%8][1];
continue;
}
if((map[i1][j1]==ch && map[i2][j2]!='1') || (map[i1][j1]!=ch && map[i2][j2]=='1')){
flag=true;
return 0;
}
if(map[i1][j1]==ch && map[i2][j2]=='1'){
is_visit[i1][j1]=global;
is_same(i1,j1,i2,j2,offset,ch);
}
i1-=dir[i][0];
j1-=dir[i][1];
i2-=dir[(i+offset)%8][0];
j2-=dir[(i+offset)%8][1];
}
return 0;
}
int is_same1(int i1,int j1,int i2,int j2,int offset,char ch){
if(flag) return 0;
is_visit[i1][j1]=global;
if(map[i1][j1]!=ch || map1[i2][j2]!='1'){
flag=true;
return 0;
}
for(int i=0;i<8;i++){
i1+=dir[i][0];
j1+=dir[i][1];
i2+=dir[(i+offset)%8][0];
j2+=dir[(i+offset)%8][1];
if(is_visit[i1][j1]==global){
i1-=dir[i][0];
j1-=dir[i][1];
i2-=dir[(i+offset)%8][0];
j2-=dir[(i+offset)%8][1];
continue;
}
if((map[i1][j1]==ch && map1[i2][j2]!='1') || (map[i1][j1]!=ch && map1[i2][j2]=='1')){
flag=true;
return 0;
}
if(map[i1][j1]==ch && map1[i2][j2]=='1'){
is_visit[i1][j1]=global;
is_same1(i1,j1,i2,j2,offset,ch);
}
i1-=dir[i][0];
j1-=dir[i][1];
i2-=dir[(i+offset)%8][0];
j2-=dir[(i+offset)%8][1];
}
return 0;
}
int change(){
int i,j,k;
for(i=0;i<150;i++){
map[n+1][i]=map1[n+1][i]=0;
}
for(i=1;i<=n;i++){
map1[i][m+1]=0;
for(j=m,k=1;j>=1;j--,k++)
map1[i][j]=map[i][k];
}
return 0;
}
int init(int i,int j,char ch){
map[i][j]=ch;
for(int a=0;a<8;a++){
if(map[i+dir[a][0]][j+dir[a][1]]=='1') {
map[i+dir[a][0]][j+dir[a][1]]=ch;
init(i+dir[a][0],j+dir[a][1],ch);
}
}
return 0;
}
int main(){
int i,j,k,t,p,q,r;
char ch;
scanf("%d",&t);
memset(map,0,sizeof(map));
memset(map1,0,sizeof(map1));
while(t--){
global=1;
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++)
scanf("%s",map[i]+1);
change();
flag=false;
memset(is_visit,0,sizeof(is_visit));
ch='a';
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
if(map[i][j]=='1'){
init(i,j,ch);
for(p=i;p<=n;p++)
for(q=0;q<=m;q++){
if(map[p][q]=='1'){
for(r=0;r<4;r++){
flag=false;
is_same(i,j,p,q,r*2,ch);
global++;
if(flag==false){
init(p,q,ch);
global++;
break;
}
flag=false;
is_same1(i,j,p,m-q+1,r*2,ch);
if(flag==false){
init(p,q,ch);
global++;
break;
}
global++;
}
global++;
}
}
ch++;
}
}
for(i=1;i<=n;i++) printf("%s\n",map[i]+1);
// for(i=1;i<=n;i++) printf("%s\n",map1[i]+1);
printf("\n");
}
return 0;
}
相关文章推荐
- 【转】【C#】判断两个文件是否相同
- htmlparser 分析网页:判断是否相同(仅限cc98)
- 1.4 写一个函数判断两个字符串是否使用相同的字符构成。
- 判断两个字符串是否有相同的字符
- 判断两棵树在不考虑子树顺序时是否相同
- C语言判断两个数组是否有相同的元素
- 判断UIImage是否相同
- java中如何高效判断两个容器是否有相同元素(时间复杂度为O(1))
- [iOS开发]判断两个数组中数据是否相同
- LeetCode刷题之三:判断两个二叉树是否相同
- 【二叉树】判断两棵树是否相同
- java怎么判断两个Set 里的对象的值是否相同
- 【c++系列】typeid操作符(判断类型是否相同)
- 判断MAC地址与预设值是否相同
- HashSet如何判断加入的元素是否相同?
- Junit的使用入门,判断控制台输出是否与预期相同
- 判断一个字段是否有相同的数据
- 判断两棵二叉树是否结构相同
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、3223都
- PHP判断数组中是否存在相同的值