HDU--3885[Find the Difference] 暴搜
来源:互联网 发布:洛克人exe3放错数据 编辑:程序博客网 时间:2024/06/02 06:54
题意:
就是QQ里的大家来找茬= =!。给两个n*m的整数矩形,求两个矩形间不同的小矩形,输出个数以及每个矩形的左上角、右下角的坐标。要求每个独立的小矩形满足至少有一个点不相同,他的周围一圈是相同的,且这个小矩形不能在其余这种矩形之内。
思路:
由于数据较小(N,M<=50)枚举每一个点,如果当前点是不同点,则从该点向外BFS,扩展出一个矩形。最后对所有矩形按要求排序,然后去掉一些被覆盖的矩形,输出即可。
PS.居然把qsort的比较函数写错了,查了一个晚上,唉,最近做题越来越不顺了 = =!
CODE:
/*BFS*//*AC代码:46ms*/#include <iostream>#include <cstdio>#include <memory.h>#include <algorithm>#include <queue>#define MAXN 55#define min(a,b) (a<b?a:b)#define max(a,b) (a>b?a:b)using namespace std;struct Rect{ int x1,y1,x2,y2;}rect[55*55];struct Point{ int x,y; Point(int x1,int y1) {x=x1;y=y1;} Point(){}};int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};int map1[MAXN][MAXN],map2[MAXN][MAXN];bool vis[MAXN][MAXN];bool flag[55*55];int cnt;//矩形个数int N,M,cas;queue<Point>Q;int cmp(const void *p1,const void *p2){ if(((struct Rect *)p1)->x1!=((struct Rect *)p2)->x1) return ((struct Rect *)p1)->x1-((struct Rect *)p2)->x1; if(((struct Rect *)p1)->y1!=((struct Rect *)p2)->y1) return ((struct Rect *)p1)->y1-((struct Rect *)p2)->y1; if(((struct Rect *)p1)->x2!=((struct Rect *)p2)->x2) return ((struct Rect *)p2)->x2-((struct Rect *)p1)->x2; return ((struct Rect *)p2)->y2-((struct Rect *)p1)->y2;}void Init(){ int i,j; char s[300]; for(i=1;i<=N;i++) { for(j=1;j<=M;j++) scanf("%d",&map1[i][j]); } for(i=1;i<=N;i++) { for(j=1;j<=M;j++) scanf("%d",&map2[i][j]); }}bool Judge(int x,int y){ if(x>=1&&x<=N&&y>=1&&y<=M) return true; return false;}void Run(int dx,int dy,Rect &now){ int i; Q.push(Point(dx,dy)); if(dx<now.x1||dx>now.x2) { for(i=now.y1;i<=now.y2;i++) Q.push(Point(dx,i)); } if(dy<now.y1||dy>now.y2) { for(i=now.x1;i<=now.x2;i++) Q.push(Point(i,dy)); } now.x1=min(now.x1,dx); now.y1=min(now.y1,dy); now.x2=max(now.x2,dx); now.y2=max(now.y2,dy);}void BFS(int sx,int sy){ int i,x,y; while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); vis[sx][sy]=true; rect[cnt].x1=rect[cnt].x2=sx; rect[cnt].y1=rect[cnt].y2=sy; Q.push(Point(sx,sy)); while(!Q.empty()) { x=Q.front().x; y=Q.front().y; Q.pop(); for(i=0;i<8;i++) { int dx=x+dir[i][0]; int dy=y+dir[i][1]; if(Judge(dx,dy)&&!vis[dx][dy]) { vis[dx][dy]=true; if(map1[dx][dy]!=map2[dx][dy]) { Run(dx,dy,rect[cnt]); } } } } cnt++;}void Solve(){ int i,j; cnt=0; for(i=1;i<=N;i++) { for(j=1;j<=N;j++) { if(map1[i][j]!=map2[i][j]) { BFS(i,j); } } } qsort(rect,cnt,sizeof(rect[0]),cmp); memset(flag,true,sizeof(flag)); int num=0; for(i=0;i<cnt;i++) { if(!flag[i]) continue; for(j=i+1;j<cnt;j++) { if(!flag[j]) continue; if(rect[i].x1<=rect[j].x1&&rect[i].y1<=rect[j].y1&&rect[i].x2>=rect[j].x2&&rect[i].y2>=rect[j].y2) { flag[j]=false; } } } for(i=0;i<cnt;i++) num+=flag[i]; printf("%d\n",num); for(i=0;i<cnt;i++) { if(flag[i]) printf("%d %d %d %d\n",rect[i].x1,rect[i].y1,rect[i].x2,rect[i].y2); }}int main(){ while(scanf("%d%d",&N,&M)!=EOF) { Init(); Solve(); }return 0;}/*5 50 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 10 0 1 0 00 1 0 1 00 0 1 0 01 0 0 0 011 1 5 5*/
- HDU--3885[Find the Difference] 暴搜
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- Find the Difference
- 160.Find the Difference
- LeetCode389. Find the Difference
- Find the Difference
- 389. Find the Difference
- Leetcode:Find the Difference
- leetcode_389 Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- Find the Difference --LeetCode
- [389]Find the Difference
- 389. Find the Difference
- LeetCode Find the Difference
- 关于可不可以在头文件中使用unnamed namespace
- 封闭开发第四五天
- 坦克大战中的几个难点总结
- 自动化测试工具—“魔爪”的束缚,“思想”的剥离
- URL特殊字符需转义
- HDU--3885[Find the Difference] 暴搜
- android linux kernel和standard linux kernel的区别
- i2c驱动调试技巧(1)
- 厦门归来
- 将文本文件的内容或者文字保存成图片
- CreateUserWizard 的密码命名规则修改
- android获得手机的电量
- Silverlight加载xap后通过反射相互调用方法及元素
- ExtJS 4.1有什么值得期待?