
10189 - Minesweeper
Moderator: Board moderators
-
- New poster
- Posts: 8
- Joined: Fri Jan 04, 2008 5:57 pm
I got WA,plz help
Last edited by ChainRule on Wed Jan 09, 2008 5:12 pm, edited 1 time in total.
424(WA)
WHy am i getting WA?? any help
Code: Select all
#include<iostream>
using namespace std;
int check(int i, int j, int x, int y, char b[][105]);
main()
{
int i,j,k=0,m,n,z;
char a[105][105];
while(1)
{
cin >> m >> n;
if(m==0&&n==0)
break;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin >> a[i][j];
k++;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(a[i][j]!='*')
{
z=check(i,j,m-1,n-1,a);
a[i][j]=z;
}
cout << "Field #" << k << ":\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout << a[i][j];
cout << "\n";
}
cout << "\n";
}
}
int check(int i, int j, int x, int y, char b[][105])
{
int k=i-1,l=i+1,m=j-1,n=j+1,p=0,q,w;
char a[105][105];
for(q=0;q<=x;q++)
for(w=0;w<=y;w++)
a[q][w]=b[q][w];
if(i==0)
k=0;
if(i==x)
l=i;
if(j==0)
m=0;
if(j==y)
n=j;
if(a[k][m]=='*')
{
a[k][m]=-1;
p++;
}
if(a[i][m]=='*')
{
a[i][m]=-1;
p++;
}
if(a[l][m]=='*')
{
a[l][m]=-1;
p++;
}
if(a[k][j]=='*')
{
a[k][j]=-1;
p++;
}
if(a[l][j]=='*')
{
a[l][j]=-1;
p++;
}
if(a[k][n]=='*')
{
a[k][n]=-1;
p++;
}
if(a[i][n]=='*')
{
a[i][n]=-1;
p++;
}
if(a[l][n]=='*')
{
a[l][n]=-1;
p++;
}
return (p+48);
}
-
- New poster
- Posts: 3
- Joined: Fri Feb 29, 2008 11:39 am
Gtting WA for my Code
Hello,
I am getting Wrong Answer for this code. Don't know what is the problem.Please help.
and I tested all the cases which are present in this forum. It has passed all of them.
<Code>
#include<stdio.h>
#include<stdlib.h>
#define MAXFIELD_SIZE 100
struct field {
unsigned int fno;
unsigned int n;
unsigned int m;
};
int main()
{
unsigned int **mw; /* Minesweeper */
unsigned int *a;
unsigned int n,m,i,j,inchar,c=0,x=0;
unsigned int ***wf;
struct field *fp;
fp = (struct field*)malloc(MAXFIELD_SIZE*sizeof(struct field));
scanf("%u %u",&fp[x].n,&fp[x].m);
wf = (unsigned int***) malloc(MAXFIELD_SIZE* sizeof(unsigned int**));
while(fp[x].n != 0 && fp[x].m != 0)
{
fp[x].fno = x; /* Assigning the field no */
n = fp[x].n;
m = fp[x].m;
if (n<=0 && m>100)
exit -1;
a = (unsigned int *) malloc(n * m * sizeof(unsigned int));
if(a == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}
mw = (unsigned int**) malloc(n * sizeof(unsigned int*));
if(mw == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}
for(i=0;i<n;i++)
mw = a + m *i;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
while((inchar = getchar()) == ' ' || inchar == '\n' || inchar == '\t');
mw[j] = inchar;
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(mw[j] == '.')
mw[j] = 0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (mw[j] == '*')
{
if(i>=1)
{
if(mw[i-1][j]!='*')
mw[i-1][j]++;
}
if(i>=1 && j<m-1)
{
if(mw[i-1][j+1]!='*')
mw[i-1][j+1]++;
}
if(i>=1 && j>=1)
{
if(mw[i-1][j-1]!='*')
mw[i-1][j-1]++;
}
if(j<m-1)
{
if(mw[j+1] !='*')
mw[j+1]++;
}
if(j>=1)
{
if(mw[j-1] !='*')
mw[j-1]++;
}
if(i<n-1)
{
if(mw[i+1][j] !='*')
mw[i+1][j]++;
}
if(i<n-1 && j<m-1)
{
if(mw[i+1][j+1] != '*')
mw[i+1][j+1]++;
}
if(i<n-1 && j>=1)
{
if(mw[i+1][j-1]!= '*')
mw[i+1][j-1]++;
}
}
}
}
wf[x]=mw;
x++;
scanf("%u %u",&fp[x].n,&fp[x].m);
}
printf("\n");
/* Printing the output */
for(c=0;c<x;c++)
{
printf("Field #%u:\n",c+1);
mw=wf[c];
n = fp[c].n;
m = fp[c].m;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mw[j] == '*')
printf("%c",mw[i][j]);
else
printf("%u",mw[i][j]);
}
printf("\n");
}
printf("\n");
}
/*freeing the allocated memory*/
for(c=0;c<x;c++)
free(wf[c]);
free(wf);
free(fp);
return 0;
}
I am getting Wrong Answer for this code. Don't know what is the problem.Please help.
and I tested all the cases which are present in this forum. It has passed all of them.
<Code>
#include<stdio.h>
#include<stdlib.h>
#define MAXFIELD_SIZE 100
struct field {
unsigned int fno;
unsigned int n;
unsigned int m;
};
int main()
{
unsigned int **mw; /* Minesweeper */
unsigned int *a;
unsigned int n,m,i,j,inchar,c=0,x=0;
unsigned int ***wf;
struct field *fp;
fp = (struct field*)malloc(MAXFIELD_SIZE*sizeof(struct field));
scanf("%u %u",&fp[x].n,&fp[x].m);
wf = (unsigned int***) malloc(MAXFIELD_SIZE* sizeof(unsigned int**));
while(fp[x].n != 0 && fp[x].m != 0)
{
fp[x].fno = x; /* Assigning the field no */
n = fp[x].n;
m = fp[x].m;
if (n<=0 && m>100)
exit -1;
a = (unsigned int *) malloc(n * m * sizeof(unsigned int));
if(a == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}
mw = (unsigned int**) malloc(n * sizeof(unsigned int*));
if(mw == NULL)
{
printf("No memory for Minesweeper\n");
exit -2;
}
for(i=0;i<n;i++)
mw = a + m *i;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
while((inchar = getchar()) == ' ' || inchar == '\n' || inchar == '\t');
mw[j] = inchar;
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(mw[j] == '.')
mw[j] = 0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (mw[j] == '*')
{
if(i>=1)
{
if(mw[i-1][j]!='*')
mw[i-1][j]++;
}
if(i>=1 && j<m-1)
{
if(mw[i-1][j+1]!='*')
mw[i-1][j+1]++;
}
if(i>=1 && j>=1)
{
if(mw[i-1][j-1]!='*')
mw[i-1][j-1]++;
}
if(j<m-1)
{
if(mw[j+1] !='*')
mw[j+1]++;
}
if(j>=1)
{
if(mw[j-1] !='*')
mw[j-1]++;
}
if(i<n-1)
{
if(mw[i+1][j] !='*')
mw[i+1][j]++;
}
if(i<n-1 && j<m-1)
{
if(mw[i+1][j+1] != '*')
mw[i+1][j+1]++;
}
if(i<n-1 && j>=1)
{
if(mw[i+1][j-1]!= '*')
mw[i+1][j-1]++;
}
}
}
}
wf[x]=mw;
x++;
scanf("%u %u",&fp[x].n,&fp[x].m);
}
printf("\n");
/* Printing the output */
for(c=0;c<x;c++)
{
printf("Field #%u:\n",c+1);
mw=wf[c];
n = fp[c].n;
m = fp[c].m;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mw[j] == '*')
printf("%c",mw[i][j]);
else
printf("%u",mw[i][j]);
}
printf("\n");
}
printf("\n");
}
/*freeing the allocated memory*/
for(c=0;c<x;c++)
free(wf[c]);
free(wf);
free(fp);
return 0;
}
-
- New poster
- Posts: 15
- Joined: Mon Mar 31, 2008 1:20 am
- Location: Egypt
- Contact:
Re: 10189 awful really awful WA!!!!!!
i get WA and i didn't know my error
help me pleaaas
this is my code
thanx in advance
help me pleaaas

this is my code
Code: Select all
# include <iostream>
using namespace std;
int main()
{
int numberOfRows,numberOfColumns,counter=1;
while(cin>>numberOfRows>>numberOfColumns)
{
if (numberOfRows == 0 && numberOfColumns == 0)
break;
char board[110][110];
int MS[110][110]={0};
for (int i=0;i<numberOfRows;i++)
for (int j=0;j<numberOfColumns;j++)
cin>>board[i][j];
for(int i=0;i<numberOfRows;i++)
{
for (int j=0;j<numberOfColumns;j++)
{
int a=i+1,b=j+1;
if (board[i][j] == '*')
{
MS[a][b+1]++;
MS[a][b-1]++;
MS[a-1][b]++;
MS[a+1][b]++;
MS[a-1][b+1]++;
MS[a-1][b-1]++;
MS[a+1][b+1]++;
MS[a+1][b-1]++;
}
}
}
cout<<"Field #"<<counter<<':'<<endl;
for(int i=0;i<numberOfRows;i++)
{
for (int j=0;j<numberOfColumns;j++)
{
int a=i+1,b=j+1;
if (board[i][j] == '*')
cout<<board[i][j];
else
cout<<MS[a][b];
}
cout<<endl;
}
cout<<endl;
counter++;
}
return 0;
}
Re: 10189 - Minesweeper
The problem states...
But you are printing an empty line after each input set.There must be an empty line between field outputs.
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 15
- Joined: Mon Mar 31, 2008 1:20 am
- Location: Egypt
- Contact:
Re: 10189 - Minesweeper
thanx Jan i get AC in Minesweeper but now I get WA in Mine SweeperJan wrote:The problem states...But you are printing an empty line after each input set.There must be an empty line between field outputs.

I waiot your advice in the other thread thanx very much
Re: 10189 - Minesweeper
I can't understand why i m getting w a?
PLZZ help...
here's the code:
PLZZ help...
here's the code:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int r,c,k;
char **ch;
for(k=1;;k++)
{
scanf("%d %d",&r,&c);
if(r==0&&c==0)
{
break;
}
int i,j,count;
ch=(char **)malloc(r*sizeof(char*));
for(i=0;i<r;i++)
{
ch[i]=(char *)malloc(c+1);
}
for(i=0;i<r;i++)
{
scanf("%s",ch[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;
if(ch[i][j]=='.')
{
if(i+1<r&&ch[i+1][j]=='*')
{
count++;
}
if(j+1<r&&ch[i][j+1]=='*')
{
count++;
}
if(i+1<r&&j+1<c&&ch[i+1][j+1]=='*')
{
count++;
}
if(i+1<r&&j-1>=0&&ch[i+1][j-1]=='*')
{
count++;
}
if(i-1>=0&&j+1<c&&ch[i-1][j+1]=='*')
{
count++;
}
if(i-1>=0&&ch[i-1][j]=='*')
{
count++;
}
if(j-1>=0&&ch[i][j-1]=='*')
{
count++;
}
if(i-1>=0&&j-1>=0&&ch[i-1][j-1]=='*')
{
count++;
}
ch[i][j]=count+'0';
}
}
}
printf("\nField #%d:\n",k);
for(i=0;i<r;i++)
{
printf("%s\n",ch[i]);
}
for(i=0;i<r;i++)
{
free(ch[i]);
}
free(ch);
}
return 0;
}
Re: 10189 - Minesweeper
You are not checking the 8 directions correctly. For example,
And of course you can simplify the method by describing a function like
Now just call the function for all 8 directions. Hope these help.
Code: Select all
if(j+1<r&&ch[i][j+1]=='*') // why j+1<r? shouldn't it be j+1<c?
Code: Select all
int pos(int x,int y)
{
if(x>=0 && y>=0 && x<r && y<c)
if(array[x][y]=='*') return 1;
return 0;
}
Ami ekhono shopno dekhi...
HomePage
HomePage
Re: 10189 - Minesweeper
still getting w a...what can i do further?where's the mistake? is that in output format?
#include <stdio.h>
#include <stdlib.h>
int pos(int x,int y,char **ch,int r,int c)
{
if(x>=0 && y>=0 && x<r && y<c)
{
if(ch[x][y]=='*')
return 1;
}
return 0;
}
int main ()
{
int r,c,k;
char **ch;
for(k=1;;k++)
{
scanf("%d %d",&r,&c);
if(r==0&&c==0)
{
break;
}
int i,j,count;
ch=(char **)malloc(r*sizeof(char*));
for(i=0;i<r;i++)
{
ch=(char *)malloc(c+1);
}
for(i=0;i<r;i++)
{
scanf("%s",ch);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;
if(ch[j]=='.')
{
if(pos(i+1,j,ch,r,c))
{
count++;
}
if(pos(i,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j+1,ch,r,c))
{
count++;
}
if(pos(i-1,j,ch,r,c))
{
count++;
}
if(pos(i,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j-1,ch,r,c))
{
count++;
}
ch[j]=count+'0';
}
}
}
printf("\nField #%d:\n",k);
for(i=0;i<r;i++)
{
printf("%s\n",ch);
}
for(i=0;i<r;i++)
{
free(ch);
}
free(ch);
}
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int pos(int x,int y,char **ch,int r,int c)
{
if(x>=0 && y>=0 && x<r && y<c)
{
if(ch[x][y]=='*')
return 1;
}
return 0;
}
int main ()
{
int r,c,k;
char **ch;
for(k=1;;k++)
{
scanf("%d %d",&r,&c);
if(r==0&&c==0)
{
break;
}
int i,j,count;
ch=(char **)malloc(r*sizeof(char*));
for(i=0;i<r;i++)
{
ch=(char *)malloc(c+1);
}
for(i=0;i<r;i++)
{
scanf("%s",ch);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
count=0;
if(ch[j]=='.')
{
if(pos(i+1,j,ch,r,c))
{
count++;
}
if(pos(i,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j+1,ch,r,c))
{
count++;
}
if(pos(i+1,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j+1,ch,r,c))
{
count++;
}
if(pos(i-1,j,ch,r,c))
{
count++;
}
if(pos(i,j-1,ch,r,c))
{
count++;
}
if(pos(i-1,j-1,ch,r,c))
{
count++;
}
ch[j]=count+'0';
}
}
}
printf("\nField #%d:\n",k);
for(i=0;i<r;i++)
{
printf("%s\n",ch);
}
for(i=0;i<r;i++)
{
free(ch);
}
free(ch);
}
return 0;
}
Re: 10189 - Minesweeper
You are printing a blank line after each input. But this shouldn't be...
There should be blank line after each test case.
It should be like this...
Remove the code After Accepted.
There should be blank line after each test case.
It should be like this...
Code: Select all
5 5
<<-------a blank line(I used, but you didn't)
*****
*...*
*.*.*
*...*
***** <<---------no blank line(you are using blank line, but I didn't)
Field #1:
*****
*646*
*4*4*
*646*
*****
4 4
*.*.
.***
**..
.*.*
Field #2:
*4*3
4***
**63
3*3*
Remove the code After Accepted.

try_try_try_try_&&&_try@try.com
This may be the address of success.
This may be the address of success.