Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
两次广搜怎么wa?帮小弟看看吧#include<iostream> #include<vector> using namespace std; char map[1000][1000]; //map[row][column] int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; struct Point { int x; int y; }; short dist[1000][1000]; vector<Point> vec; Point temppoint; int startx,starty; int max_dist; void search(int i,int j,int distance) { int k; for(k=0;k<4;k++) { if(map[i+dir[k][0]][j+dir[k][1]]=='.') { map[i+dir[k][0]][j+dir[k][1]]='1'; temppoint.x=i+dir[k][0]; temppoint.y=j+dir[k][1]; vec.push_back(temppoint); dist[i+dir[k][0]][j+dir[k][1]]=distance+1; max_dist=distance+1; startx=i+dir[k][0]; starty=j+dir[k][1]; } } } int main() { int n,x,y,j,k,l; cin>>n; while(n--) { memset(dist,0,sizeof(dist)); memset(map,'\0',sizeof(map)); startx=starty=0; max_dist=0; cin>>x>>y; //y row x column for(j=0;j<y;j++) cin>>map[j]; k=l=0; while(map[k][l]=='#') //找到第一个'.',一列一列的找 { if(k<y-1) k++; else { k=0; l++; } } map[k][l]='1'; temppoint.x=k; temppoint.y=l; vec.clear(); vec.push_back(temppoint); int qh=0; while(qh<vec.size()) { search(vec[qh].x,vec[qh].y,dist[vec[qh].x][vec[qh].y]); qh++; } for(k=0;k<y;k++) { for(l=0;l<x;l++) { if(map[k][l]=='1') map[k][l]='.'; } } memset(dist,0,sizeof(dist)); vec.clear(); temppoint.x=startx; temppoint.y=starty; map[startx][starty]='1'; vec.push_back(temppoint); max_dist=0; qh=0; while(qh<vec.size()) { search(vec[qh].x,vec[qh].y,dist[vec[qh].x][vec[qh].y]); qh++; } cout<<"Maximum rope length is "<<max_dist<<"."<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator