2019-05-05 14:12:15 C++

C++

Copy Copied! Full
#include <iostream> using namespace std; #define QUEUE_SIZE 100000 #define next(a) (((a)+1)%QUEUE_SIZE) int front,rear; int queue[QUEUE_SIZE]; void enqueue(int x){ if(next(rear)==front){ exit(1); } queue[rear]=x; rear = next(rear); } int dequeue(){ if(front==rear){ exit(1); } int x = queue[front]; front = next(front); return x; } int empty(){ return front==rear; } int score[100][100]; void init2(){ for(int i=0;i<100;i++)for(int j=0;j<100;j++)score[i][j]=0; } int getscore(){ int temp = 0; for(int i=0;i<100;i++){ for(int j=0;j<100;j++){ temp = max(temp,score[i][j]); } } return temp; } int main(void){ int W,H; int ans = 0; cin >> W >> H; char map[H][W+1]; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin>> map[i][j]; } } for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(map[i][j]=='1'){ init2(); enqueue(i*1000+j); score[i][j]=1; while(empty()==0){ int temp = dequeue(); int x = temp/1000; int y = temp%1000; int dx[4]={0,0,1,-1}; int dy[4]={-1,1,0,0}; for(int i=0;i<4;i++){ if(x+dx[i]<0 || x+dx[i]>=H ||y+dy[i]<0||y+dy[i]>=W)continue; if(map[x+dx[i]][y+dy[i]]=='1'&&score[x+dx[i]][y+dy[i]]==0){ score[x+dx[i]][y+dy[i]] = score[x][y]+1; enqueue(x+dx[i]*1000+y+dy[i]); } } } for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cout << score[i][j]; } cout << endl; } cout << endl; ans = max(ans,getscore()); }else continue; } } cout << ans; }
RECOMMEND