poj1175 搜索+hash

来源:互联网 发布:java aes 256 cbc 编辑:程序博客网 时间:2024/06/15 04:51

题意:划分,对称或旋转相同的算同一种

分析:bfs划分区间,hash平均距离

#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <cstring>#include <cmath>#include <iostream>using namespace std;#define maxn 105*105int way[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};int mp[105][105];map<int,long long int >M;map<int,char>IC;int ID;int n,m;int num;struct node{    int x,y;};int check(node s){    if( mp[s.x][s.y] == 1 )return 1;        return 0;}node data[maxn];void bfs(int x,int y){    num = 0 ;    node s;    s.x=x,s.y=y;    queue<node>Q;    Q.push(s);    while( !Q.empty() ){        node e = Q.front();        Q.pop();        mp[e.x][e.y] = ID;        data[++num].x = e.x;        data[num].y = e.y;        for(int i = 0 ; i < 8 ; ++i){            node nx = e;            nx.x += way[i][0];            nx.y += way[i][1];            if(check(nx)){                mp[nx.x][nx.y] = ID;                Q.push(nx);            }        }    }}void Deal(int x,int y){    double xx = 0;    double yy = 0;    double tot = 0;    bfs(x,y);    for(int i = 1 ; i <= num ; ++i){        xx += data[i].x;        yy += data[i].y;    }    xx = xx / num;    yy = yy / num ;    for(int i = 1 ; i <= num ; ++i){        node e = data[i];        tot += pow(e.x-xx,4) + pow(e.y-yy,4);    }    M[ID] = (long long int)(tot*1000);}void init(){    M.clear();    ID = 1;    IC.clear();}int main(){    while( cin >> m >> n ){        init();        for(int i = 1 ; i <= n ; ++i)            for(int j = 1 ; j <= m ; ++j)                scanf("%1d",&mp[i][j]);        for(int i = 1 ; i <= n ; ++i)            for(int j = 1 ; j <= m ; ++j){                if( mp[i][j] == 1 ){                    ID++;                    Deal(i,j);                }            }       int k = 0;       for(int i = 2 ; i <= ID ; ++i){           if( 'a' <= IC[i] && IC[i] <= 'z')continue;           IC[i] = 'a' + k++;           for(int j = i + 1 ; j <= ID ; ++j){               if( M[i] == M[j] ){                   IC[j] = IC[i];               }           }       }        for(int i = 1 ; i <= n ; ++i){            for(int j = 1 ; j <= m ; ++j){                if( mp[i][j] ){                    printf("%c",IC[mp[i][j]]);                }               else printf("0");            }printf("\n");        }    }}


0 0
原创粉丝点击