Can someone clarify this?
![:roll:](./images/smilies/icon_rolleyes.gif)
Moderator: Board moderators
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 105
typedef struct queue queue;
struct queue
{
int x, y;
queue * next;
} * head, * curr, * tail;
char map[MAX][MAX];
void createQueue( void )
{
head = curr = tail = NULL;
}
void enqueue( const int x, const int y )
{
curr = ( queue * ) malloc ( sizeof( queue ) );
curr->x = x;
curr->y = y;
if ( head == NULL )
head = tail = curr;
else
{
tail->next = curr;
tail = curr;
}
tail->next = NULL;
}
void dequeue( int & x, int & y )
{
if ( head != NULL )
{
x = head->x;
y = head->y;
curr = head;
head = head->next;
free( curr );
}
}
int floodFill( int x, int y, const int rows, const int cols )
{
int i = 0, counter = 0;
int ax[8] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int ay[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
if ( map[x][y] == '0' )
{
createQueue();
enqueue( x, y );
map[x][y] = '1';
}
while ( head != NULL )
{
dequeue( x, y );
counter++;
// search from north till north west
for ( i = 0; i < 8; i++ )
if ( x + ax[i] >= 0 && x + ax[i] < rows &&
y + ay[i] >= 0 && y + ay[i] < cols &&
map[ x + ax[i] ][ y + ay[i] ] == '0' )
{
enqueue( x + ax[i], y + ay[i] );
map[ x + ax[i] ][ y + ay[i] ] = '1';
}
}
return counter;
}
void printMap( int m, int n )
{
int i = 0, j = 0;
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < n; j++ )
putchar( map[i][j] );
putchar(' \n' );
}
putchar(' \n' );
}
int main()
{
int cases = 0, x = 0, y = 0, i = 0, k = 0, rows = 0, cols = 0;
char input = 0, dummy = 0;
scanf( "%d\n", &cases );
for ( k = 0; k < cases; k++ )
{
if ( k )
printf( " \n" );
memset( map[0], '\0', sizeof( map[0] ) );
gets( map[0] );
for ( i = 0, x = 0; map[0][i] == ' '; i++ );
while ( map[0][i] != ' ' )
{
x *= 10;
x += ( map[0][i] - 48 );
i++;
}
while ( map[0][i] == ' ' ) i++;
y = 0;
while ( map[0][i] != '\0' )
{
y *= 10;
y += ( map[0][i] - 48 );
i++;
}
memset( map[0], '1', sizeof( map[0] ) );
memset( map[1], '1', sizeof( map[1] ) );
rows = 1; cols = 1;
while ( scanf( "%c", &input ) == 1 )
{
if ( input == '\n' )
{
if ( dummy == ' ' )
break;
else
{
rows++; i = cols; cols = 1;
memset( map[rows], '1', sizeof( map[rows] ) );
}
}
else if ( input != ' ' )
map[rows][cols++] = input;
dummy = input;
}
rows++;
cols = strlen( map[0] );
//printMap( rows, i );
printf( " %d\n", floodFill( x, y, rows, i + 1 ) );
}
return 0;
}
Code: Select all
3
02 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
01 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
06 07
1001101
0011111
0001001
1100011
1111111
1100110
1110111
Code: Select all
12
0
1
Code: Select all
4
02 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
01 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
09 09
1001101
0011111
0001001
1100011
1111111
1100110
1110111
06 07
1001101
0011111
0001001
1100011
1111111
1100110
1110111
Code: Select all
12
0
0
1
Code: Select all
01 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
Code: Select all
rc| 012345678
--+-----------
0 | 111111111
1 | 110011011
2 | 100111111
3 | 100010011
4 | 111000111
5 | 111111111
6 | 111001101
7 | 111101111
8 | 111111111
r = row, c = column
Now you can see why you get wrong answer. The answer can be never 0. Your second and third sample input is wrong.We are interested in finding the area of a region of horizontally or vertically connected "water" cells totally enclosed by a boundary of "land" cells, given the location of a "water" cell in the region.
Code: Select all
11
02 01
1001101
0011111
0001001
1100011
1111111
1100110
1110111
01 06
1001101
0011111
0001001
1100011
1111111
1100110
1110111
06 07
1001101
0011111
0001001
1100011
1111111
1100110
1110111
06 04
1001101
0011111
0001001
1100011
1111111
1100110
1110111
06 07
100110111
001111111
000100111
110001101
111111110
110011001
111011110
110011101
06 03
100110111
001111111
000100111
110001101
111111110
110011001
111011110
110011101
04 08
100110111
001111111
000100111
110001101
111111110
110011001
111011110
110011101
02 02
111
101
111
02 03
10101
01010
10101
05 06
101010101010
010101010101
101010101010
010101010101
101010101010
010101010101
101010101010
010101010101
101010101010
010101010101
101010101010
010101010101
101010101010
05 05
00000
00000
00000
00000
00000
Code: Select all
12
1
1
3
6
5
6
1
7
78
25
So you need to travel in 4 directions only. Don't travel diagonally. Your output for your sample should beWe are interested in finding the area of a region of horizontally or vertically connected "water" cells totally enclosed by a boundary of "land" cells
Code: Select all
class Main
{
static int mat[][];
static int c;
static String ReadLn (int max)
{
byte linea[] = new byte [max];
int largo = 0, car = -1;
try{
while (largo < max){
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
linea [largo++] += car;
}
}
catch (IOException e){
return (null);
}
if ((car < 0) && (largo == 0)) return (null); // eof -->appears not to return eof
return (new String (linea, 0, largo));
}
public static void main (String args[])
{
Main myWork = new Main();
myWork.Begin();
}
void Begin()
{
String input;
StringTokenizer idata;
String fila;
int casos,nrofila,x,y;
mat=new int[102][102];
input=Main.ReadLn (255);
idata = new StringTokenizer (input);
casos=Integer.parseInt(idata.nextToken());
input=Main.ReadLn (255);//lee un enter
while (casos>0){
for (int i=0;i<=101;i++)
for (int j=0;j<=101;j++)
mat[i][j]=1;
nrofila=0;
c=0;
input=Main.ReadLn (255);//lee coordenadas
idata = new StringTokenizer (input);
x=Integer.parseInt(idata.nextToken());
y=Integer.parseInt(idata.nextToken());
while ((input=Main.ReadLn(255))!=null){//mientras haya datos los lee y los copia a la matriz
nrofila++;
if (input.length()<=1) break;
idata = new StringTokenizer (input);
fila=idata.nextToken();
for (int j=1;j<=fila.length();j++){
mat[nrofila][j]=fila.charAt(j-1)-48;
}
}
//cuenta la cantidad de agua
cuenta(x,y);
//imprime el numero
System.out.println(c);
casos--;
}
}
public void cuenta(int x,int y){
if (mat[x][y]==2){
//ya ha sido visitado
}else if(mat[x][y]==1){
//es tierra
}else if(mat[x][y]==0){
//es agua
c++;
mat[x][y]=2;//lo visita
cuenta(x-1,y);
cuenta(x+1,y);
cuenta(x,y-1);
cuenta(x,y+1);
}
}
}
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
private int[][] field;
private int[][] moves = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
private int moveCount = 4;
private static int MAX_SIZE = 101;
private ArrayList<int[]> queue;
private int waterCount;
public static void main(String[] args) throws IOException {
Main m = new Main();
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String line = reader.readLine();
line = line.trim();
int count = Integer.parseInt(line);
reader.readLine();
for (int i = 0; i < count; i++) {
m.fillField();
line = reader.readLine();
line = line.trim();
int sX = Integer.parseInt(line.substring(0, 2));
int sY = Integer.parseInt(line.substring(3, 5));
m.setStart(sX, sY);
line = null;
int c = 0;
while ((line = reader.readLine()) != null) {
c++;
line = line.trim();
if(line.length()<=0 || line.length()>MAX_SIZE-2){
break;
}
for (int j=0;j<line.length();j++){
if (line.charAt(j)=='0'){
m.addToField(c, j+1, 0);
}
if (line.charAt(j)=='1'){
m.addToField(c, j+1, 1);
}
}
}
m.markField();
//m.showField();
m.printWaterCount();
}
}
public void addToField(int x, int y, int value){
field[x][y] = value;
}
public void printWaterCount() {
if (waterCount>0){
System.out.println(" "+waterCount);
System.out.println(" ");
}
}
public void setStart(int x, int y){
addToQueue(x, y);
}
public void fillField() {
waterCount = 0;
queue = new ArrayList<int[]>();
field = new int[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++) {
for (int j = 0; j < MAX_SIZE; j++) {
field[i][j] = 2;
}
}
}
/*
public void showField() {
for (int i = 0; i < MAX_SIZE; i++) {
for (int j = 0; j < MAX_SIZE; j++) {
if (field[i][j]!=2){
System.out.print(field[i][j]);
}
}
System.out.println("");
}
}
*/
public void markField() {
while (queue.size() > 0) {
int[] item = removeFormQueue();
int x = item[0];
int y = item[1];
if (field[x][y] == 0) {
waterCount++;
field[x][y] = 3;
for (int i = 0; i < moveCount; i++) {
int mX = x + moves[i][0];
int mY = y + moves[i][1];
if (field[mX][mY] == 0) {
addToQueue(mX, mY);
}
}
}
}
}
public void addToQueue(int x, int y) {
int[] p = { x, y };
queue.add(p);
}
public int[] removeFormQueue() {
int[] head = queue.get(queue.size() - 1);
queue.remove(queue.size() - 1);
return head;
}
}
Code: Select all
12
1
1
3
2
5
1
1
1
1
25