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

@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

10189 - Minesweeper

Post by @ce »

getting WA.....getting correct o/p for sample cases...plzz help

Code: Select all

Code removed after AC
Last edited by @ce on Mon Jun 25, 2012 10:00 am, edited 2 times in total.
-@ce
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

arr[n][m+1] should be limited to arr[0 through n-1][0 through m]. You have i from 0 to n-1 and then you're checking if i!=n which will always be true. You should check if i!=n-1.
Check input and AC output for thousands of problems on uDebug!
punpunm
New poster
Posts: 2
Joined: Fri Jun 01, 2012 8:03 am

10189 - Minesweeper

Post by punpunm »

I have got wrong answer...Orz

Code: Select all

#include<stdio.h>

int height,width;
char map[101][101];
int caseNo=1;

void readMap(void);
void calcMap(void);
void printMap(void);

int main(int argc, char** argv){
	while(1){
		readMap();
		if(height==0 && width==0)
			break;
		calcMap();
		printMap();
	}
	return 0;
}

void readMap(void){
	int i;
	scanf("%d %d",&height,&width);
	for(i=0;i<height;i++){
		scanf("%s",&map[i]);
	}
}

void calcMap(void){
	int y, x, count;
	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			if (map[y][x] == '.') {
				count = 0;
				if (y == 0) {
					if (x == 0) {
						if (map[y][x + 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
						if (map[y + 1][x + 1] == '*') {
							count++;
						}
					} else if (x < width - 1) {
						if (map[y][x - 1] == '*') {
							count++;
						}
						if (map[y][x + 1] == '*') {
							count++;
						}
						if (map[y + 1][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
						if (map[y + 1][x + 1] == '*') {
							count++;
						}
					} else {
						if (map[y][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
					}
				} else if (y < height - 1) {
					if (x == 0) {
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y - 1][x + 1] == '*') {
							count++;
						}
						if (map[y][x + 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
						if (map[y + 1][x + 1] == '*') {
							count++;
						}
					} else if (x < width - 1) {
						if (map[y - 1][x - 1] == '*') {
							count++;
						}
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y - 1][x + 1] == '*') {
							count++;
						}
						if (map[y][x - 1] == '*') {
							count++;
						}
						if (map[y][x + 1] == '*') {
							count++;
						}
						if (map[y + 1][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
						if (map[y + 1][x + 1] == '*') {
							count++;
						}
					} else {
						if (map[y - 1][x - 1] == '*') {
							count++;
						}
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x - 1] == '*') {
							count++;
						}
						if (map[y + 1][x] == '*') {
							count++;
						}
					}
				} else {
					if (x == 0) {
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y - 1][x + 1] == '*') {
							count++;
						}
						if (map[y][x + 1] == '*') {
							count++;
						}
					} else if (x < width - 1) {
						if (map[y - 1][x - 1] == '*') {
							count++;
						}
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y - 1][x + 1] == '*') {
							count++;
						}
						if (map[y][x - 1] == '*') {
							count++;
						}
						if (map[y][x + 1] == '*') {
							count++;
						}
					} else {
						if (map[y - 1][x - 1] == '*') {
							count++;
						}
						if (map[y - 1][x] == '*') {
							count++;
						}
						if (map[y][x - 1] == '*') {
							count++;
						}
					}
				}
				map[y][x] = count + '0';
			}
		}
	}
}

void printMap(void){
	int i,j;
	
	if(caseNo>1)
		printf("\n");
	printf("Field #%d:\n",caseNo++);
	for(i=0;i<height;i++){
		for(j=0;j<width;j++){
			printf("%c",map[i][j]);
		}
		printf("\n");
	}
}
@ce
Learning poster
Posts: 71
Joined: Mon May 28, 2012 8:46 am
Location: Ranchi, India

Re: 10189 - Minesweeper

Post by @ce »

thanks a lot....i got AC :)
-@ce
punpunm
New poster
Posts: 2
Joined: Fri Jun 01, 2012 8:03 am

Re: 10189 - Minesweeper

Post by punpunm »

I have got AC, THX
armankashef
New poster
Posts: 10
Joined: Tue Jun 05, 2012 9:33 pm

why WA?

Post by armankashef »

my code is:


#include <iostream>
#include <string.h>
using namespace std;
char ch[101][101];
int mat[101][101] , dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}} , n , m ;
void f()
{
memset( mat , 0 , sizeof( mat ) );
int i , j , k , r , s;
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
for( k = 0 ; k < 8 ; k ++ )
{
r = i + dir[k][0];
s = j + dir[k][1];
if( ( r >= 0 && s >= 0 && r < n && s < m ) && ch[j] == '*' )
mat[r][s] ++;
}
}
int main()
{
int i , j , c = 1;
while( cin >> n >> m , n || m )
{
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
cin >> ch[j];
f();
cout << "Field #" << c ++ << ':' << endl;
for( i = 0 ; i < n ; i ++ )
{
for( j = 0 ; j < m ; j ++ )
{
if( ch[j] == '*' )
cout << '*';
else
cout << mat[j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Don't print a blank line at the end of the output.
Check input and AC output for thousands of problems on uDebug!
armankashef
New poster
Posts: 10
Joined: Tue Jun 05, 2012 9:33 pm

10189 Wrong answer

Post by armankashef »

this is my code for 10189
I don't know why wrong answer!!!
Help me.

Code: Select all

#include <iostream>
#include <string.h>
using namespace std;
char ch[101][101];
int mat[101][101] , dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}} , n , m ;
void f()
{
	memset( mat , 0 , sizeof( mat ) );
	int i , j , k , r , s;
	for( i = 0 ; i < n ; i ++ )
		for( j = 0 ; j < m ; j ++ )
			for( k = 0 ; k < 8 ; k ++ )
			{
				r = i + dir[k][0];
				s = j + dir[k][1];
				if( ( r >= 0 && s >= 0 && r < n && s < m ) && ch[i][j] == '*' )
					mat[r][s] ++;
			}
}
int main()
{
	int i , j , c = 1;
	while( cin >> n >> m , n || m )
	{
		for( i = 0 ; i < n ; i ++ )
			for( j = 0 ; j < m ; j ++ )
				cin >> ch[i][j];
		f();
		cout << "Field #" << c ++ << ':' << endl;
		for( i = 0 ; i < n ; i ++ )
		{
			for( j = 0 ; j < m ; j ++ )
			{
				if( ch[i][j] == '*' )
					cout << '*';
				else
					cout << mat[i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 Wrong answer

Post by brianfry713 »

Don't print a newline at the end of the output.
Check input and AC output for thousands of problems on uDebug!
armankashef
New poster
Posts: 10
Joined: Tue Jun 05, 2012 9:33 pm

Re: 10189 Wrong answer

Post by armankashef »

accepted
thank you very much :D
luimiruiz
New poster
Posts: 1
Joined: Tue Jul 03, 2012 7:05 am

Re: 10189 - Minesweeper WA???

Post by luimiruiz »

Code: Select all

#include <iostream>
using namespace std;
int main()
{
	int dx[] = { 1, 1, 1, 0, 0, -1, -1, -1 };
	int dy[] = { 1, -1, 0, 1, -1, 1, 0, -1 };
	int n, m, x = 1;
	char aux;
	while(1)
	{
		cin >>n >> m;
		if(n == 0 && m == 0)return 0;
		char arr[102][102];
		for(int i = 1; i <= n; ++i)
		{
			for(int j = 1; j <= m; ++j)
			{
				cin >> aux;
				if(aux == '.')
					arr[i][j]='0';
				else
					arr[i][j]= aux;
			}
		}
		for(int i = 1; i <= n; ++i)
		{
			for(int j = 1; j <= m; ++j)
			{
				if(arr[i][j] != '*')continue;
				for(int x = 0; x < 8; ++x)
				{
					if(arr[i+dx[x]][j+dy[x]] != '*')
						arr[i+dx[x]][j+dy[x]]++;
				}
			}
		}
		cout << "Field #" << x <<':'<<endl;
		++x;
		for(int i = 1; i <= n; ++i)
		{

			for(int j = 1; j <= m; ++j)
			{
				cout << arr[i][j];

			}
			cout << endl;
		}
		cout << endl;
	}
}

Why WA????????? I dont understand
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Don't print an extra newline at the end of the output.
Check input and AC output for thousands of problems on uDebug!
sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 10189 - Minesweeper

Post by sumit saha shawon »

My all input and output is ok but why I am getting WA.
My code:

#include<stdio.h>
#include<string.h>
char field[105][105];
int main()
{
int r,c,a=1,kase=1;
while(scanf("%d %d",&r,&c)==2)
{
if(r==0&&c==0)
break;
// memset(field,0,)

int i,j,count=0;
for(i=0; i<r; i++)
scanf("%s",&field);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(field[j]=='*')
continue;
else
{
if(field[j-1]=='*')
count++;
if(field[j+1]=='*')
count++;
if(field[i+1][j]=='*')
count++;
if(field[i-1][j]=='*')
count++;
if(field[i+1][j-1]=='*')
count++;
if(field[i+1][j+1]=='*')
count++;
if(field[i-1][j+1]=='*')
count++;
if(field[i-1][j-1]=='*')
count++;
}
field[j]=count+'0';
count=0;
}
}
if(a!=1)
puts("");
//kase++;
printf("Field #%d:\n",kase++);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
printf("%c",field[j]);
puts("");

}

a=2;
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Check your array boundaries. If i=j=0, don't try to read field[i-1][j-1].
Check input and AC output for thousands of problems on uDebug!
s.mahzabin
New poster
Posts: 2
Joined: Mon Aug 27, 2012 12:15 pm

Re: 10189 - Minesweeper

Post by s.mahzabin »

why am I getting wrong answer? :-? :(
this is my code-


#include<stdio.h>
#define MAX 20000

int main()
{
char c, field[MAX];
int m,n,i,x,j,mine[MAX],k;
for(x = 1; (scanf("%d%d", &m, &n) != 2 ||(m != 0 && n != 0)); ++x)
{

i = j = 0;
while(i < (m * n))
{
c = getchar();
if( c == '.')
field[i++] = '0';
else if(c == '*')
{
field[i++] = c;
mine[j++] = i-1;
}
}
for(--j; j >= 0; --j)
{
k = mine[j];

if(((k+1) %n) != 0)
{
if(field[k+1] != '*')
++field[k+1];
if((k - n) >= 0 && field[(k-n)+1] != '*')
{
++field[(k - n) + 1];
}
if((k + n) < (m * n) && field[k+n+1] != '*')
{
++field[k+n+1];
}
mine[j] = 0;
}
if(((k+1) %n) != 1)
{
if(field[k-1] != '*')
++field[k-1];
if((k - n) >= 0 && field[k-n-1] != '*')
++field[(k-n) -1];
if((k + n) < (m * n) && field[k+n-1] != '*')
++field[(k+n) -1];
}
if((k-n) >= 0 )
{
if(field[k-n] != '*')
++field[k-n];
}
if((k+n) < (m*n))
{
if(field[k+n] != '*')
++field[k+n];
}

}
if(x > 1)
putchar('\n');
printf("Field #%d:\n", x);
for(i = 0; i < (m*n); ++i)
{
printf("%c", field);
if(((i+1)%n) == 0)
putchar('\n');
field = 0;
}
}
return 0;
}
Post Reply

Return to “Volume 101 (10100-10199)”