FZU2150-Fire Game
来源:互联网 发布:c语言初学者看什么书 编辑:程序博客网 时间:2024/04/30 00:58
题目链接:FZU2150
题目背景挺无聊的……吐槽一下
坑,FZU不支持C++11 巨坑
我大概CE了3小时。用迭代器的时候解引用 得用*(it).x 不能用it->x 看了一下午才发现是这里CE
先扫一遍看有几个连通分量,
如果连通分量大于2,直接输出-1
如果连通分量有两个,那么对于每一个连通分量分别取起点当作点火的位置,然后暴力即可。
如果连通分量只有一个,那么从这个连通分量中任取两个点暴力,注意,两个点是可以重合的哦(WA过2次)。
AC代码:
#include <iostream>#include <map>#include <set>#include <string>#include<string.h>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector>using namespace std;const int maxn=15;char mp[maxn][maxn];bool vis[maxn][maxn];int t[maxn][maxn];int T,n,m,color;bool fire[maxn][maxn];struct node{ int x,y; int time;};struct dot{ int x,y;};void dfs(int x,int y,int type){ if(type==-1) color++; vis[x][y]=1; t[x][y]=color; if(x-1>=0&&!vis[x-1][y]&&mp[x-1][y]=='#') dfs(x-1,y,color); if(x+1<n&&!vis[x+1][y]&&mp[x+1][y]=='#') dfs(x+1,y,color); if(y-1>=0&&!vis[x][y-1]&&mp[x][y-1]=='#') dfs(x,y-1,color); if(y+1<m&&!vis[x][y+1]&&mp[x][y+1]=='#') dfs(x,y+1,color); return;}int bfs(int x1,int y1,int x2,int y2){ memset(fire,false,sizeof(fire)); queue<node> q; node t1,t2,t3; t1.x=x1; t1.y=y1; t1.time=0; q.push(t1); fire[x1][y1]=1; t1.x=x2; t1.y=y2; t1.time=0; q.push(t1); fire[x2][y2]=1; int ans_time=0; while(!q.empty()){ t2=q.front(); q.pop(); int tx=t2.x; int ty=t2.y; ans_time=max(ans_time,t2.time); if(tx-1>=0&&mp[tx-1][ty]=='#'&&!fire[tx-1][ty]){ fire[tx-1][ty]=1; t3.x=tx-1; t3.y=ty; t3.time=t2.time+1; q.push(t3); } if(tx+1<n&&mp[tx+1][ty]=='#'&&!fire[tx+1][ty]){ fire[tx+1][ty]=1; t3.x=tx+1; t3.y=ty; t3.time=t2.time+1; q.push(t3); } if(ty-1>=0&&mp[tx][ty-1]=='#'&&!fire[tx][ty-1]){ fire[tx][ty-1]=1; t3.x=tx; t3.y=ty-1; t3.time=t2.time+1; q.push(t3); } if(ty+1<m&&mp[tx][ty+1]=='#'&&!fire[tx][ty+1]){ fire[tx][ty+1]=1; t3.x=tx; t3.y=ty+1; t3.time=t2.time+1; q.push(t3); } } return ans_time;}int main(){ // freopen("I.txt","r",stdin); scanf("%d",&T); for(int i=0;i<T;i++){ scanf("%d%d",&n,&m); // /* memset(mp,'\0',sizeof(mp)); memset(vis,false,sizeof(vis)); memset(t,0,sizeof(t)); color=0; //*/ for(int j=0;j<n;j++){ scanf("%s",mp[j]); } // puts(""); //for(int j=0;j<n;j++) printf("%s\n",mp[j]); for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ if(!vis[j][k]&&mp[j][k]=='#'){ dfs(j,k,-1); }else if(!vis[j][k]&&mp[j][k]=='.'){ vis[j][k]=1; } } } /*check fenlei*/ /* printf("**\n"); for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ printf("%d ",t[j][k]); }puts(""); }*/ if(color>2) printf("Case %d: -1\n",i+1); else if(color==2){ //set<pair<int,int> > st1,st2; vector<dot> st1,st2; for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ if(t[j][k]==1){ dot tmp; tmp.x=j; tmp.y=k; st1.push_back(tmp); } //st1.insert({j,k}); else if(t[j][k]==2){ dot tmp; tmp.x=j; tmp.y=k; // st1.push_back(tmp); st2.push_back(tmp); } //st2.insert({j,k}); } } int ans=9999; for(vector<dot>::iterator it1=st1.begin();it1!=st1.end();it1++){ for(vector<dot>::iterator it2=st2.begin();it2!=st2.end();it2++){ int x1,y1,x2,y2; x1=(*it1).x; y1=(*it1).y; x2=(*it2).x; y2=(*it2).y; int temp=bfs(x1,y1,x2,y2); if(temp<ans) ans=temp; // printf("temp=%d\n",temp); } } printf("Case %d: %d\n",i+1,ans); /*check set*/ /* printf("jihe1\n"); for(auto it=st1.begin();it!=st1.end();it++) printf("(%d,%d)\n",it->first,it->second); printf("jihe2\n"); for(auto it=st2.begin();it!=st2.end();it++){ printf("(%d,%d)\n",it->first,it->second); } */ }else if(color==1){ //set<pair<int,int> > st1; vector<dot> st1; for(int j=0;j<n;j++){ for(int k=0;k<m;k++){ if(t[j][k]==1){ dot tmp; tmp.x=j; tmp.y=k; st1.push_back(tmp); // st1.push_back({j,k}); } // st1.insert({j,k}); } } int ans=9999; for(vector<dot>::iterator it1=st1.begin();it1!=st1.end();it1++){ for(vector<dot>::iterator it2=it1;it2!=st1.end();it2++){ // it2++;//这个地方一直WA // if(it2==st1.end()) break; int x1,y1,x2,y2; x1=(*it1).x; y1=(*it1).y; x2=(*it2).x; y2=(*it2).y; int temp=bfs(x1,y1,x2,y2); if(temp<ans) ans=temp; // printf("temp=%d\n",temp); } } printf("Case %d: %d\n",i+1,ans); } } return 0;}
阅读全文
0 0
- FZU2150 Fire Game (bfs)
- FZU2150 Fire Game
- FZU2150 Fire Game
- FZU2150 Fire Game
- fzu2150 Fire Game bfs
- FZU2150 Fire Game (BFS)
- FZU2150-Fire Game
- FZU2150 Fire Game
- FZU2150 Fire Game(BFS)
- FZU2150(Fire Game)(枚举+BFS)
- FZU2150 Fire Game(搜索题:bfs)
- fzu2150——Fire Game(BFS)
- FZU2150 Fire Game(两点广搜)
- FZU2150 Fire Game —— BFS
- FZU2150 fire
- FZU2150 fire 双搜bfs
- Fire Game
- Fire Game
- 网络流模板题
- 日期函数
- centos系统中挂载盘和扩容硬盘的方法
- C# 类的索引器
- .Net网站架构设计(七)网络安全
- FZU2150-Fire Game
- 隐式Intent实现Activity跳转
- 图论作业
- 页面刷新的时候记住滚动条的位置
- 使用EMWIN过程中遇到的问题与解决方法
- 考研英语重点汇总
- 字符函数
- Spring boot 简介
- .Net网站架构设计(四) 缓存技术