10189 - Minesweeper

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

amrupam
New poster
Posts: 2
Joined: Mon Mar 16, 2009 7:23 pm

Re: 10189 - Minesweeper

Post by amrupam »

I dont know why it does not work
anyone's help will be appriciated

Code: Select all

#include <stdio.h>
int board[100][100];
int nrow, ncol;
void init_board(int nrow,int ncol){
     int j;
     for(int i=0;i<nrow;i++)
         for(j=0;j<ncol;j++)
              board[i][j]=0;                   
}

bool isValid(int i,int j)
{
     if(i<0 || i>(nrow-1) || j<0 || j>(ncol-1)) return false;
     return true;
}

void print_board(int count)
{
     int j;
     printf("Field #%d:\n",count);
      for(int i=0;i<nrow;i++){
            for(j=0;j<ncol;j++)  
            {
                  if(board[i][j]<0) printf("*");
                  else printf("%d",board[i][j]);               
            }
            if(i<(nrow-1))printf("\n");
      }
      //printf("\n");
}

int main() {
    
	char ch;
	int j,count=0;
    //freopen ( "10189.in" , "r" , stdin ) ;
    //freopen ( "10189.out" , "w" , stdout ) ;    
	while (1) {
        scanf("%d %d",&nrow,&ncol);
        init_board(nrow,ncol);
        if(nrow==0 && ncol==0) break; 
        if(count!=0)printf("\n\n");       
        count++;
        for(int i=0;i<nrow;i++){
            for(j=0;j<ncol;j++)                
            {
                                                 
                  scanf("%c",&ch);
                  
                  while(ch==10) scanf("%c",&ch);
                  if(ch=='.') continue;
                  if(ch == '*'){ board[i][j]=-100;
                      if(isValid(i-1,j-1)) board[i-1][j-1]++;
                      if(isValid(i-1,j)) board[i-1][j]++;
                      if(isValid(i-1,j+1)) board[i-1][j+1]++;
                      if(isValid(i,j-1)) board[i][j-1]++;
                      if(isValid(i,j+1)) board[i][j+1]++;
                      if(isValid(i+1,j-1)) board[i+1][j-1]++;
                      if(isValid(i+1,j)) board[i+1][j]++;
                      if(isValid(i+1,j+1)) board[i+1][j+1]++;
                  }          
            }            
        } 
        print_board(count);   
   }      	
}     

My codes works perfectly with the following

iuput:

Code: Select all

6 10
****..**.*
.**.....*.
**...****.
..***.*.**
**.*..*.*.
*..*...***

4 3
.*.
***
.*.
*.*

1 2
*.

7 1
.
.
.
.
*
*
.

7 6
...*..
..**..
.***.*
...*..
***.*.
******
****.*

5 10
*.**..*.**
*...*.*.*.
*.****...*
.*.....*.*
****..**.*

8 8
*.*****.
.**.*..*
**......
.***..*.
*.*.*..*
*..*...*
**..*..*
*....*.*

1 2
..

4 10
....*.....
*.....**..
****.*..**
**.***..*.

6 4
....
..**
.*.*
...*
*...
*.**

5 2
**
**
.*
*.
**

7 8
......*.
*......*
*.*...*.
...***..
*..***..
*..*.***
*.**..*.

8 7
..*...*
......*
.***...
*..*...
**.****
*..*..*
.**...*
...*...

9 6
****..
*.**.*
**.*.*
*.**.*
****..
****..
**.*..
.**..*
..****

1 4
*.**

7 9
..*..*...
.......*.
.*.*.****
...*...*.
**.**.**.
.....*..*
****.***.

9 3
***
.*.
**.
*..
*..
***
*..
***
**.

8 5
**...
.****
*.***
***..
..**.
..**.
....*
...**

2 10
..****.***
....***.**

5 1
*
*
.
*
.
0 0
my output:

Code: Select all

Field #1:
****11**3*
5**32356*3
**543****4
45***5*7**
**5*43*6*5
*33*212***

Field #2:
3*3
***
4*4
*3*

Field #3:
*1

Field #4:
0
0
0
1
*
*
1

Field #5:
013*20
13**41
1***4*
356*42
***6*3
******
****5*

Field #6:
*3**23*4**
*546*5*4*4
*4****334*
4*65434*5*
****11**4*

Field #7:
*4*****2
4**5*43*
**642222
4***22*2
*5*5*23*
*53*323*
**22*24*
*3112*3*

Field #8:
00

Field #9:
1101*22210
*43333**32
****5*44**
**5***22*3

Field #10:
0122
12**
1*5*
223*
*333
*3**

Field #11:
**
**
4*
*4
**

Field #12:
110001*2
*311023*
*3*333*2
233***31
*23***52
*44*6***
*3**33*3

Field #13:
01*102*
134312*
2***211
*56*532
**4****
*55*45*
2**322*
123*111

Field #14:
****31
*7**5*
**6*6*
*7**5*
****41
****30
**7*31
3**55*
13****

Field #15:
*2**

Field #16:
01*11*211
1232335*3
1*3*3****
335*546*4
**3**3**3
45545*65*
****3***2

Field #17:
***
5*4
**2
*41
*52
***
*74
***
**3

Field #18:
**432
4****
*7***
***63
25**2
02**3
0135*
001**

Field #19:
01****4***
0124***4**

Field #20:
*
*
2
*
1

poixp
New poster
Posts: 20
Joined: Mon Apr 07, 2008 11:05 am

Re: 10189 - Minesweeper

Post by poixp »

You missed end line symbol at last line of output.

daichin85
New poster
Posts: 3
Joined: Tue Jul 01, 2008 1:29 pm

WA

Post by daichin85 »

Hi all, i got WA, but i don't understand why. Please help. my code is.

Code: Select all

#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
	int p,q;
	int k,l;
	for(p = -1; p <= 1; p++)
		for(q = -1; q <= 1; q++)
			if(p != 0 || q != 0){
				k = p + i;
				l = q + j;
				if(k >= 0 && k <= n && l >= 0 && l <= m){
					if(output[k][l] == '.')
						output[k][l] = 0;
					if(output[k][l] != '*')
						output[k][l]++;
				}		
			}		
}

void print(int n,int m, int count){
	int i,j;
	printf("Field #%d:\n",count);
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++)
			if(output[i][j] == '*')
				printf("*");
			else
				printf("%d",output[i][j]);
			printf("\n");
	}
}

int main(){
	int n,m;
	int count = 0;
	char row[100];
	while(1){
		fgets(row, sizeof row, stdin);
		sscanf(row, "%d%d", &n, &m);
		if((n == 0)&&(m == 0))
			break;
		else{
			count++;
			int i,j;
			for(i = 0; i < n; i++)
				fgets(output[i], sizeof output, stdin);
			for(i = 0; i < n; i++)
				for(j = 0; j < m; j++){
					if(output[i][j] == '.')
						output[i][j] = 0;
					if(output[i][j] == '*')
						solve(n,m,i,j);
				}		
			print(n,m,count);		
		}	
	}
	return 0;
}

daichin85
New poster
Posts: 3
Joined: Tue Jul 01, 2008 1:29 pm

WA

Post by daichin85 »

i got wrong answer. please help me. here is my code

Code: Select all


#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
	int p,q;
	int k,l;
	for(p = -1; p <= 1; p++)
		for(q = -1; q <= 1; q++)
			if(p != 0 || q != 0){
				k = p + i;
				l = q + j;
				if(k >= 0 && k <= n && l >= 0 && l <= m){
					if(output[k][l] == '.')
						output[k][l] = 0;
					if(output[k][l] != '*')
						output[k][l]++;
				}		
			}		
}

void print(int n,int m, int count){
	int i,j;
	printf("Field #%d:\n",count);
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++)
			if(output[i][j] == '*')
				printf("*");
			else
				printf("%d",output[i][j]);
			printf("\n");
	}
}

int main(){
	int n,m;
	int count = 0;
	char row[100];
	while(1){
		fgets(row, sizeof row, stdin);
		sscanf(row, "%d%d", &n, &m);
		if((n == 0)&&(m == 0))
			break;
		else{
			count++;
			int i,j;
			for(i = 0; i < n; i++)
				fgets(output[i], sizeof output, stdin);
			for(i = 0; i < n; i++)
				for(j = 0; j < m; j++){
					if(output[i][j] == '.')
						output[i][j] = 0;
					if(output[i][j] == '*')
						solve(n,m,i,j);
				}		
			print(n,m,count);		
		}	
	}
	return 0;
}


doorman
New poster
Posts: 1
Joined: Fri Apr 03, 2009 6:24 pm

unknown runtime error

Post by doorman »

I've been looking to my code for a few days now and I just can't figure out what's the reason for the runtime error judgement (it would help a lot if I could at least see what error does the judge get, but since I can't...)

Anyway, the problem is the minesweeper one (10189). The judge compiles it with gcc 4.1.2. whereas I use 4.3.4.

Please help me 'cause this RTE it driving me nuts!

Here's the code:

Code: Select all

#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>


#define MS_MINE_NUMBER -1000
#define MS_MINE_SYMBOL '*'


using namespace std;


class Minesweeper {
	
	private:
		int** board;
		int noRows;
		int noCols;
		int boardNumber;
		int currentRow;
		void mineFound( int x, int y );
	
	public:
		Minesweeper( int rows, int cols, int boardNo );
		~Minesweeper();
		void addRow( string line );
		void compute();
		void printBoard( ostream& output );
	
};


Minesweeper::Minesweeper( int rows, int cols, int boardNo ) {
	
	int i;
	
	this->noRows = rows;
	this->noCols = cols;
	this->boardNumber = boardNo;
	this->currentRow = 1;
	
	this->board = new int*[this->noRows + 2];
	
	for( i = 0; i < this->noRows + 2; i++ )
		this->board[i] = new int[this->noCols + 2];
	
}


Minesweeper::~Minesweeper() {
	
	int i;
	
	for( i = 0; i < this->noRows + 2; i++ )
		delete this->board[i];
	
	delete[] this->board;
	
}


void Minesweeper::addRow( string line ) {
	
	int strIter;
	
	for( strIter = 0; strIter < line.length(); strIter++ )
		if( line[strIter] == MS_MINE_SYMBOL )
			this->board[this->currentRow][strIter + 1] = MS_MINE_NUMBER;
		else
			this->board[this->currentRow][strIter + 1] = 0;
	
	this->currentRow++;
	
}


void Minesweeper::mineFound( int x, int y ) {
	
	int rowIter, colIter;
	
	for( rowIter = x - 1; rowIter < x + 2; rowIter++ ) {
		for( colIter = y - 1; colIter < y + 2; colIter++ ) {
			if( rowIter == x && colIter == y )
				continue;
			this->board[rowIter][colIter]++;
		}
	}
	
}


void Minesweeper::compute() {
	
	int rowIter, colIter;
	
	for( rowIter = 0; rowIter < this->noRows + 2; rowIter++ ) {
		for( colIter = 0; colIter < this->noCols + 2; colIter++ )
			if( this->board[rowIter][colIter] < 0 )
				this->mineFound( rowIter, colIter );
	}
	
}


void Minesweeper::printBoard( ostream& output ) {
	
	int rowIter, colIter;
	
	if( this->boardNumber > 1 )
		output << endl;
	
	output << "Field #" << this->boardNumber << ":" << endl;
	
	for( rowIter = 1; rowIter < this->noRows + 1; rowIter++ ) {
		for( colIter = 1; colIter < this->noCols + 1; colIter++ )
			if( this->board[rowIter][colIter] < 0 )
				output << MS_MINE_SYMBOL;
			else
				output << this->board[rowIter][colIter];
		output << endl;
	}
	
}



int main() {
	
	Minesweeper* ms;
	int x, y, i, num = 0;
	string size, row;
	
	while( getline( cin, size ) ) {
		istringstream dims( size );
		dims >> x >> y;
		if( x == 0 || y == 0 )
			break;
		num++;
		ms = new Minesweeper( x, y, num );
		for( i = 0; i < x; i++ ) {
			getline( cin, row );
			ms->addRow( row );
		}
		ms->compute();
		ms->printBoard( cout );
	}
	
	return 0;
	
}

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: 10189 - Minesweeper

Post by mohitkanwal »

Hi all I keep getting WA everytime I submit the problem again and again .. I searched the entire forum .. tested all the data that I could find in the forum and they are all correct .. I wanted to know whether it's okay to store the computed hint and display later coz .. I tried to switch to 1 entry and 1 display format ...still WA .. At last when everything has crashed I have posted my code here .. Plzz help ... I have patiently calculated grids upto 20 X 20 and still find WA...

Code: Select all

//removed after AC
Last edited by mohitkanwal on Tue Apr 07, 2009 8:53 pm, edited 1 time in total.

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: WA

Post by mohitkanwal »

daichin85 wrote:i got wrong answer. please help me. here is my code

Code: Select all


#include<stdio.h>

char output[100][100];

void solve(int n,int m, int i, int j){
	int p,q;
	int k,l;
	for(p = -1; p <= 1; p++)
		for(q = -1; q <= 1; q++)
			if(p != 0 || q != 0){
				k = p + i;
				l = q + j;
				if(k >= 0 && k <= n && l >= 0 && l <= m){
					if(output[k][l] == '.')
						output[k][l] = 0;
					if(output[k][l] != '*')
						output[k][l]++;
				}		
			}		
}

void print(int n,int m, int count){
	int i,j;
	printf("Field #%d:\n",count);
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++)
			if(output[i][j] == '*')
				printf("*");
			else
				printf("%d",output[i][j]);
			printf("\n");
	}
}

int main(){
	int n,m;
	int count = 0;
	char row[100];
	while(1){
		fgets(row, sizeof row, stdin);
		sscanf(row, "%d%d", &n, &m);
		if((n == 0)&&(m == 0))
			break;
		else{
			count++;
			int i,j;
			for(i = 0; i < n; i++)
				fgets(output[i], sizeof output, stdin);
			for(i = 0; i < n; i++)
				for(j = 0; j < m; j++){
					if(output[i][j] == '.')
						output[i][j] = 0;
					if(output[i][j] == '*')
						solve(n,m,i,j);
				}		
			print(n,m,count);		
		}	
	}
	return 0;
}

Try to use larger array It might help

kunalghosh
New poster
Posts: 1
Joined: Sun Apr 26, 2009 12:02 pm

Need help with WA in 10189 - Minesweeper

Post by kunalghosh »

Code: Select all

#include<iostream>
int const SIZE=110;
class matrix{
    char field[SIZE][SIZE];
    int row,col;
    matrix *link;

public:
    matrix(){
        row=col=0;
        link=NULL;
    }
    void set_row_col(int r,int c){
        row=r;col=c;link=NULL;
    }    
    matrix* ret_link(){
        return(link);
    }
    void process_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(field[i][j]=='*'){
                    for(int k=-1;k<=1;k++){
                        for(int l=-1;l<=1;l++){
                            if(is_point_in_field(i+k,j+l)){
                                if(field[i+k][j+l]=='*')
                                    continue;
                                else
                                    field[i+k][j+l]++;
                            }
                        }
                    }
                }
            }
        }
    }                        
    void get_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                std::cin>>field[i][j];
                if(field[i][j]=='.')
                    field[i][j]='0';
            }
        }
        process_field();
    }
    void disp_field(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                std::cout<<field[i][j];
            }
            std::cout<<std::endl;
        }
    }
    
    int is_point_in_field(int r,int c){
        return((r>=0 && r<row)&&(c>=0 && c<col));
    }

    void set_link(matrix *a){
        link=a;
    }
};

int main(int argc, char **argv){
    matrix *field1=NULL,*temp=NULL;
    int r,c;

    std::cin>>r>>c;
    while(r!=0 || c!=0 && ((r>=0 && r<=9)&&(c>=0 && c<=9))){
        
        matrix *a=new matrix;
        a->set_row_col(r,c);
        
        if(NULL==field1){
            temp=field1=a;
        }
        else{
            temp->set_link(a);
            temp=temp->ret_link();
        }
        a->get_field();
        std::cin>>r>>c;

        if(!((r>=0 && r<=9)&&(c>=0 && c<=9)))
            return(0);//return(1);
    }
    temp=field1;
    int i=1;

    while(NULL!=temp){
        std::cout<<"Field #"<<i++<<":"<<std::endl;
        temp->disp_field();
        temp=temp->ret_link();
        if(NULL!=temp)
            std::cout<<std::endl;
    }    
    return(0);
}
Guys i have been trying out the problem for some time now. Earlier i was getting Compile time error, for the return(1) i commented out later, now i am getting wrong answer ! i guess my answer conforms to the output required . i need help with where i might have got wrong.
regards

Flame
New poster
Posts: 1
Joined: Thu Jul 02, 2009 10:46 pm

Re: 10189 - Minesweeper

Post by Flame »

I got wrong answer. Please help me.

Code: Select all

/* 10189 - Minesweeper */
#include <stdio.h>

char Field[102][102];

void Check(int, int);
void Reset(void);

int main()
{
	
	int n, m, i, j, t;

	t = 0;	
	scanf("%d %d", &n, &m);
	while(n || m)
	{
		Reset();
		for(i = 1; i <= n; ++i)
			scanf("%s",&Field[i][1]);
		++t;
		printf("\nField #%d:\n", t);
		for(i = 1; i <= n; ++i){
			for(j = 1; j <= m; ++j)
				Check(i, j);
			printf("\n");
		}
		printf("\n");
		scanf("%d %d", &n, &m);
	}
	
	return 0;
}

void Check(int n, int m)
{
	char ch = '0';
	if (Field[n][m] == '*')
		ch = '*';
	else {
		if (Field[n-1][m-1] == '*')
			++ch;
		if (Field[n-1][m] == '*')
			++ch;
		if (Field[n-1][m+1] == '*')
			++ch;
		if (Field[n][m-1] == '*')
			++ch;
		if (Field[n][m+1] == '*')
			++ch;
		if (Field[n+1][m-1] == '*')
			++ch;
		if (Field[n+1][m] == '*')
			++ch;
		if (Field[n+1][m+1] == '*')
			++ch;
	}

	printf("%c", ch);
}

void Reset(void)
{
	int i, j;
	for(i = 0; i <= 101; ++i)
		for(j = 0; j <= 101; ++j)
			Field[i][j] = ' ';
}

aznin
New poster
Posts: 1
Joined: Tue Apr 21, 2009 12:34 pm

Re: 10189 - Minesweeper

Post by aznin »

i got it wrong but it can compiled. what's wrong with the code?

Code: Select all


#include<iostream>
using namespace std;

int main (void){
    char table[100][100];
    int n, m;
    int value;
    int counter=0;
    
    do{
        cin>>n>>m;
        counter=counter+1;
        if(n==0 && m==0)
        break;
        
        for(int a=0; a<n; a++){
                for (int b=0; b<m; b++){
                    cin>>table[a][b];
                    }
        }
        
        for(int i=0; i<n; i++){
                for(int j=0; j<m; j++){
                        if(table[i][j] != '*'){
                                       value=0;
                                       for(int x=i-1; x<=i+1; x++){
                                               for(int y=j-1; y<=j+1; y++){
                                                   if(table[x][y]=='*')
                                                   value++;
                                                                           }
                                                                   }
                                               
                                               table[i][j]=value+'0';
                                               }
                                       }
                               }
                               
                               
        cout<<"Field #"<<counter<<":"<<endl;
        for(int i=0; i<n; i++){
                for(int j=0; j<m; j++){
                        cout<<table[i][j];
                                       }
                        cout<<endl;
                               }
        cout<<endl;
        }
        
        while(n!=0 && m!=0);
        

        return 0;
        }
                             
                                                                        
                                                                        
                                                              

blittman
New poster
Posts: 2
Joined: Mon Aug 31, 2009 1:13 am

Re: 10189 - Minesweeper

Post by blittman »

If your test cases work but you're getting WA, make sure your output is in this format (put a blank line ONLY BETWEEN output fields). It got accepted for me in Java:

IRO:Minesweeper Brian$ java Main
4 4
*...
....
..*.
....
3 3
*..
.*.
...
0 0
Field #1:
*100
1211
01*1
0111

Field #2:
*21
2*1
111
IRO:Minesweeper Brian$

kletoskletos
New poster
Posts: 1
Joined: Mon Sep 28, 2009 1:18 pm

10189 - Minesweeper Problem

Post by kletoskletos »

My code doesn't work and i can't figure out whats wrong with it.

Code: Select all

program p;
var
a,b,m,n,nn:integer;
Ar:array[0..101,0..101] of integer;
c:char;
begin
nn:=1;
readln(m,n);
for a:=0 to 101 do
for b:=0 to 101 do
Ar[a,b]:=0;
while (n<>0) and (m<>0) do begin
for a:=1 to M do begin
for b:=1 to N do begin
read(c);
if c='*' then begin
inc(ar[a-1,b]);
inc(ar[a-1,b-1]);
inc(ar[a,b-1]);
inc(ar[a-1,b+1]);
inc(ar[a,b+1]);
inc(ar[a+1,b]);

inc(ar[a+1,b-1]);
inc(ar[a+1,b+1]);
ar[a,b]:=-maxint;
end;
end;
readln;
end;
writeln;
writeln('Field #',nn,':');
for a:=1 to M do begin
for b:=1 to N do begin
if ar[a,b]<0 then begin
write('*');
ar[a,b]:=0;
end
else
write(ar[a,b]);
ar[a,b]:=0;
end;
writeln;
end;
inc(nn);
readln(M,N);
end;
end.
Any suggestions how to fix it?

sudipta
New poster
Posts: 11
Joined: Wed Sep 30, 2009 7:23 pm
Location: Sylhet

10189-Minesweeper (WHY WA)

Post by sudipta »

I wrote this code. output cases were correct . but WA. :x
#include<stdio.h>
int main()
{
int i,j,x,y,m,field=1;
char mine[110][110];
while(scanf("%d%d",&x,&y)==2)
{
if(x==0&&y==0)break;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%1s",&mine[j]);
}
}
if(field>1) printf("\n");
printf("Field #%d:\n",field);
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
m=0;
if(mine[j]=='*')printf("*");
else
{
if(mine[i-1][j-1]=='*')m++;
if(mine[i-1][j]=='*')m++;
if(mine[i-1][j+1]=='*')m++;
if(mine[j-1]=='*')m++;
if(mine[j+1]=='*')m++;
if(mine[i+1][j-1]=='*')m++;
if(mine[i+1][j]=='*')m++;
if(mine[i+1][j+1]=='*')m++;
if(m>0)printf("%d",m);
else if(m==0)printf("0");
}

}
printf("\n");
}
field++;
}
return 0;
}
Don't Copy, Think Also

fahim_xubayer
New poster
Posts: 5
Joined: Wed Sep 23, 2009 8:57 am

PC/UVa IDs: 110102/10189 MINESWEEPER

Post by fahim_xubayer »

can anyone please tell me what is wrong with my algorithm?
it is getting WA .in my compiler,there comes a warning everytime while compiling saying that the null characters is being ignored.however,it did produce correct answe ( test cases succeded);
#include<stdio.h>
#include<string.h>
int main(){

long int a,b,d,e,f,ar[1500][102],y,x,z=1;
char c,m[102];
while(1){
scanf("%ld%ld",&x,&y);
if((x==0)&&(y==0))
break;
for(a=0;a<=x;a++){
for(b=0;b<=y;b++)
ar[a]=0;

}
for(a=0;a<x;a++){
scanf("%s",m);

for(b=0;b<y;b++){
c=m;
e=a+1;
f=b+1;
if(c=='*'){
ar[e][f]=50;
ar[e+1][f]++;
ar[e-1][f]++;
ar[e+1][f+1]++;
ar[e-1][f+1]++;
ar[e][f+1]++;
ar[e][f-1]++;
ar[e+1][f-1]++;
ar[e-1][f-1]++;
}
}
}
printf("Field #%ld:\n",z);
for(a=1;a<=x;a++){
for(b=1;b<=y;b++){
if(ar[a]>=50){
printf("*");
continue;
}
printf("%ld",ar[a]);
}
printf("\n");
}
printf("\n");
z++;
}

return 0;
}

Taman
New poster
Posts: 32
Joined: Sun Oct 11, 2009 8:59 pm
Location: Southeast University

Re: 10189-Minesweeper (WHY WA)

Post by Taman »

Well, this program can easily be solved by using FloodFill or dfs.
Anyhow, the mistake you have made here was also made by me when I did not know Flood fill.
if(mine[i-1][j-1]=='*')m++;
if(mine[i-1][j]=='*')m++;
if(mine[i-1][j+1]=='*')m++;
if(mine[j-1]=='*')m++;
if(mine[j+1]=='*')m++;
if(mine[i+1][j-1]=='*')m++;
if(mine[i+1][j]=='*')m++;
if(mine[i+1][j+1]=='*')m++;
if(m>0)printf("%d",m);
else if(m==0)printf("0");

Well it may be the case that some grids your code is trying to check doesn't exist at all! if the current grid is mine[0][0] and your code try to check mine[i-1][j-1] then your code goes to check the grid mine[-1][-1], which is not possible! This fault leads your code to wa. it can also cause runtime error in different cases. You can check this thing recursively then it would be easier for you, as it was/is to me :D

Post Reply

Return to “Volume 101 (10100-10199)”