1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#define N 10
#define M 10
using namespace std;
int x,y,prevCoord,newCoord;
char data[N][M] = {
{'0', '0', '0', '0', '+', '+', '+', '+', '+', '+'},
{'+', '0', '0', '0', '+', '0', '0', '0', '0', '+'},
{'+', '0', '0', '0', '+', '0', '+', '+', '+', '+'},
{'+', '+', '+', '+', '+', '0', '0', '0', '0','0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0','0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '+', '+', '+','+','+','+', '0', '0', '0'},
{'0', '+', '0', '0','0', '0', '0', '0', '0', '0'},
{'0', '+', '0', '0','0', '0', '0', '0', '0', '0'},
};
void printArr(char a[N][M]){
for (int i = 0; i < N; ++i) {
cout << endl;
for (int j = 0; j < M; ++j) {
cout << a[i][j] << " ";
}
}
}
void floodFillUtil(int x, int y, int prevCoord, int newCoord)
{
if (x < 0 || x >= M || y < 0 || y >= N)
return;
if (data[x][y] != prevCoord)
return;
// Replace the color at (x, y)
data[x][y] = newCoord;
// Recur for north, east, south and west
floodFillUtil( x+1, y, prevCoord, newCoord);
floodFillUtil( x-1, y, prevCoord, newCoord);
floodFillUtil( x, y+1, prevCoord, newCoord);
floodFillUtil( x, y-1, prevCoord, newCoord);
}
void floodFill( int x, int y, int newCoord)
{
int prevCoord = data[x][y];
floodFillUtil( x, y, prevCoord, newCoord);
}
int main(){
int x,y,newcoord;
for(x=0;x<N;x++){
for(y=0;y<M;y++)
{
if(data[x][y]=='+')
floodFill(x,y,newCoord);
}
}
cout<<"longest ship"<<" "<<x<<" "<<y<<" "<<newcoord<<endl;
return 0;
}
| |