POJ 1175
//本来写了个和1021相同的HASH,但没过,于是,抱着侥幸的心理,把它变成距离的四次方,
//我就呵呵了。。。
//这个题,完全靠概率。当然了,如果是把图翻转来比较,也是可以的。但好像很麻烦。。 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int MAX=;
const int MOD=;
char map[MAX][MAX];
int dir[][]={,,-,,,,,-,,,,-,-,,-,-};
int w,h;
struct cl{
char c;
int val,next;
}clu[];
int hash[];
struct sv{
int x,y;
}save[];
int sa,counted;
int tot;
void dfs(int i,int j){
map[i][j]=''; sa++;
save[sa].x=i; save[sa].y=j;
for(int k=;k<;k++){
int tx=i+dir[k][];
int ty=j+dir[k][];
if(map[tx][ty]==''&&tx>=&&tx<h&&ty>=&&ty<w){
dfs(tx,ty);
}
}
} void calculate(){
int i,j,x,y;
int sum=;
for(i=;i<=sa;i++){
x=save[i].x; y=save[i].y;
for(j=i+;j<=sa;j++){
int dx=abs(x-save[j].x);
int dy=abs(y-save[j].y);
sum=sum+(dx*dx+dy*dy)*(dx*dx+dy*dy);
}
}
int h=sum%MOD; char sure;
if(hash[h]==-){
sure='a'+(++counted);
clu[tot].val=sum;
clu[tot].c=sure;
clu[tot].next=hash[h];
hash[h]=tot++;
}
else{
// bool flag=false;
for(int e=hash[h];e!=-;e=clu[e].next){
if(clu[e].val==sum){
clu[tot].c=clu[e].c;
clu[tot].val=sum;
clu[tot].next=hash[h];
hash[h]=tot++;
sure=clu[e].c;
// flag=true;
break;
}
}
}
for(i=;i<=sa;i++){
map[save[i].x][save[i].y]=sure;
}
} void slove(){
for(int i=;i<h;i++){
for(int j=;j<w;j++){
if(map[i][j]==''){
sa=;
dfs(i,j);
calculate();
}
}
}
} int main(){
int i;
while(scanf("%d%d",&w,&h)!=EOF){
for(i=;i<h;i++)
scanf("%s",map[i]);
counted=-; tot=;
memset(hash,-,sizeof(hash));
slove();
for(i=;i<h;i++){
printf("%s",map[i]);
printf("\n");
}
}
return ;
}
POJ 1175的更多相关文章
- poj 题目分类(1)
poj 题目分类 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K--0.50K:中短代码:0.51K--1.00K:中等代码量:1.01K--2.00K:长代码:2.01 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 转载:poj题目分类(侵删)
转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) ...
- poj和hdu部分基础算法分类及难度排序
最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
随机推荐
- 【BZOJ4555】【TJOI2016】【HEOI2016】求和
题目 传送门 解法 我们可以用容斥来求第二类斯特林数 我们知道, 第二类斯特林数\(S(n, k)\)是\(n\)个元素放进\(k\)个无标号的盒子里, 不可以含有空的. 于是我们可以考虑可以含有空的 ...
- POJ 1101 译文
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
- Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作
Windows phone7中文件的存储模式是独立的,即独立存储空间(IsolatedStorage).对文件夹与文件操作,需要借助IsolatedStorageFile类. IsolatedStor ...
- HTML 5概述
HTML语言是一种简易的文件交换标准,用于物理的文件结构,它旨在定义文件内的对象和描述文件的逻辑结构,而并不定义文件的显示.由于HTML所描述的文件具有极高的适应性,所以特别适合于WWW的出版环境. ...
- SQL中EXTRACT() 函数
EXTRACT()("提取"的意思) 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 就是返回出来具体的年,月,日 2008-12-29 16:25:46.63 ...
- OpenCV中IplImage/CvMat/Mat转化关系
原文链接:http://www.cnblogs.com/summerRQ/articles/2406109.html 如对内容和版权有何疑问,请拜访原作者或者通知本人. opencv中常见的与图像操作 ...
- numpy安装失败-小失误
1. 古老的方法: 安装python numpy库AMD64 失败,网上的教程是这样的:http://www.cnblogs.com/zhuyp1015/archive/2012 ...
- 计算laws的matlab代码
很简单的代码:不过花了codeforge上的10个点,自己写也早写出来了; 代码如下: 文件:calLaws.m function [y,h_v,h_h]=calLaws(x,id,LocalEner ...
- 用一条mysql语句插入多条数据
这篇文章主要介绍了在mysql中使用一条sql语句插入多条数据,效率非常高,但是原理其实很简单,希望对大家有所帮助 假如有一个数据表A: id name title addtime 如果需要插入n条数 ...
- grep命令总结
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹 ...