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 ...
随机推荐
- 【POJ 3460】 Booksort
[题目链接] http://poj.org/problem?id=3460 [算法] IDA* 注意特判答案为0的情况 [代码] #include <algorithm> #include ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- C# Task 源代码阅读(2)
上篇已经讲到Task 的默认的TaskScheduler 为ThreadPoolTaskScheduler. 这时我们回到原来的task 的start方法,在代码最后,调用了 ScheduleAndS ...
- 把一个文件夹下的多个csv文件合并到一个excel的多个sheet
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import pandas as pd import os import re if __name__ = ...
- python 46 盒模型 与盒模型布局
一:盒模型 1. 盒模型的概念 广义盒模型:文档中所有功能性及内容性标签,及文档中显示性标签 侠义盒模型:文档中以块级形式存在的标签(块级标签拥有盒模型100%特性且最常用) 盒模型组成:margi ...
- 复习java基础第四天(集合:List、Map、Collections、Enumeration)
一.List: List 代表一个元素有序.且可重复的集合,集合中的每个元素都有其对应的顺序索引 List 允许使用重复元素,可以通过索引来访问指定位置的集合元素. List 默认按元素的添加顺序设置 ...
- SAP computer之program counter
Program counter The program is stored in memory with the first instruction at binary address 0000, t ...
- ICCV2015上的GazeTracker论文总结
SLAM问题先慢慢编译一段时间,赶紧拾起来GazeTrack的事情...... ICCV2015的大部分paper已经可以下载,文章列表在这个位置. http://www.cvpapers.com/i ...
- 工作中总结的经验之git篇
不要以为你会git,你要知道,git不是只有commit和push 由于系统分析与设计的期末Project需要团队合作开发,因此在这里想谈谈GitHub团队项目合作开发的流程: 项目创建 项目负责人在 ...
- TimerTask定时任务
web.xml <listener> <listener-class>com.sign.listener.NFDFlightDataTaskListener</liste ...