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 ...
随机推荐
- [Codeforces 1013B] And
[题目链接] http://codeforces.com/problemset/problem/1013/B [算法] 不难发现,答案只有0,1,2,-1,共4种取值 分类讨论即可,计算时可以使用ST ...
- 玩游戏(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2566 #include <stdio.h ...
- php 关于使用七牛云存储
1.首先注册七牛云存储账号 http://www.qiniu.com/ 2.获得密钥 3.仔细查看文档 http://developer.qiniu.com/docs/v6/sdk/php-sdk.h ...
- selenium3 + python 操作浏览器基本方法
from selenium import webdriverimport time as t # driver = webdriver.Chrome()# driver.get("http: ...
- python的搜索路径与包(package)
python的搜索路径其实是一个列表,它是指导入模块时,python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功,否则导入失败: >>> import ...
- Laravel5.1 学习笔记2, 路由
安装的说明请看文档, laravel 安装 #基本路由 你将在 app/Http/routes.php 文件定义大部分路由, 这些路由将被App\Providers\RouteServiceProvi ...
- Java基础学习(二)——对象
类:是抽象的概念集合,表示的是一个共性的产物,类之中定义的是属性和行为(方法): 对象:对象是一种个性的表示,表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象. 对象=实例 对象 ...
- VM虚拟机中Ubuntu中执行apt-get update失败的解决方法(可能有效)
首先确保虚拟机是连接网络的,可以用ping命令检测一下看是否连通网络.采用nat网络的时候确保服务是开的. 如果之前执行过apt-get update命令但是失败了,执行一下 rm -rf ...
- PostgreSQL的HA解决方案-1主从和备份(master/slave and backup)
一.部署说明 1.1 实施环境 本文档实验环境如下: PGSQL主机: 192.168.1.45 PGSQL备机: 192.168.1.50 软件和系统版本 Pgsql 版本: pgsql 9.2.4 ...
- Python 遍历目录
代码: 1.递归使用遍历目录 import os def scanfile(path): filelist = os.listdir(path) allfile = [] for filename i ...