
10189 - Minesweeper
Moderator: Board moderators
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
10189 Why WA?
[c]
int main()
{
int i,j,k,l,m,a,b;
int win[200][200];
int nextx[8] = {-1,-1,-1,0,0,1,1,1};
int nexty[8] = {-1,0,1,-1,1,-1,0,1};
char string[150];
int kasus = 1;
while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);
for (a=0;a<105;a++)
for (b=0;b<105;b++)
win[a] = 0;
for (k=1;k<=i;k++) {
scanf("%s",string);
for (l=0;l<j;l++) {
if (string[l] == '*') win[k][l+1] = -1;
}
}
for (k=1;k<=i;k++){
for (l=1;l<=j;l++){
for (m=0;m<8;m++) {
if (win[k][l] != -1) {
if (win[k + nextx[m] ][l + nexty[m] ] == -1) win[k][l] = win[k][l] + 1;
}
}
}
}
if ((i==0)&&(j==0)) exit(-1);
printf ("Field #%d:\n",kasus);
kasus++;
for (k=1;k<=i;k++) {
for (l=1;l<=j;l++) {
if (win[k][l] == -1) printf ("*");
else printf ("%d",win[k][l]);
}
printf ("\n");
}
}
return 0;
}[/c]
Anyone please help me ? Why wrong answer ???
int main()
{
int i,j,k,l,m,a,b;
int win[200][200];
int nextx[8] = {-1,-1,-1,0,0,1,1,1};
int nexty[8] = {-1,0,1,-1,1,-1,0,1};
char string[150];
int kasus = 1;
while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);
for (a=0;a<105;a++)
for (b=0;b<105;b++)
win[a] = 0;
for (k=1;k<=i;k++) {
scanf("%s",string);
for (l=0;l<j;l++) {
if (string[l] == '*') win[k][l+1] = -1;
}
}
for (k=1;k<=i;k++){
for (l=1;l<=j;l++){
for (m=0;m<8;m++) {
if (win[k][l] != -1) {
if (win[k + nextx[m] ][l + nexty[m] ] == -1) win[k][l] = win[k][l] + 1;
}
}
}
}
if ((i==0)&&(j==0)) exit(-1);
printf ("Field #%d:\n",kasus);
kasus++;
for (k=1;k<=i;k++) {
for (l=1;l<=j;l++) {
if (win[k][l] == -1) printf ("*");
else printf ("%d",win[k][l]);
}
printf ("\n");
}
}
return 0;
}[/c]
Anyone please help me ? Why wrong answer ???
-
- Experienced poster
- Posts: 187
- Joined: Wed Dec 11, 2002 2:03 pm
- Location: Mount Papandayan, Garut
-
- Experienced poster
- Posts: 136
- Joined: Tue Apr 01, 2003 6:59 am
- Location: Jakarta, Indonesia
You just have to change the part:
[c]
while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);
[/c]
with
[c]
while(scanf("%i %i",&i,&j)){
if(!i&&!j) break;
[/c]
That'll solve it (but very possibly with P.E.)!!

[c]
while ((i != 0)&&(j != 0))
{
scanf("%d %d",&i,&j);
[/c]
with
[c]
while(scanf("%i %i",&i,&j)){
if(!i&&!j) break;
[/c]
That'll solve it (but very possibly with P.E.)!!


There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
10189 trouble
[cpp]
#include<iostream.h>
bool mnsp[101][101];
int m,n;
inline void set(void)
{
memset(mnsp,0,sizeof(mnsp));
}
int check(int a,int b)
{
int t = 0;
if(mnsp[a-1][b-1] == 1)
t++;
if(mnsp[a-1] == 1)
t++;
if(mnsp[a-1][b+1] == 1)
t++;
if(mnsp[a][b-1] == 1)
t++;
if(mnsp[a][b+1] == 1)
t++;
if(mnsp[a+1][b-1] == 1)
t++;
if(mnsp[a+1] == 1)
t++;
if(mnsp[a+1][b+1] == 1)
t++;
return t;
}
int main()
{
int count = 1;
cin >> m >> n;
while(m || n)
{
set();
cout << "Field #" << count << ':' << endl;
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
{
char ch;
cin >> ch;
if(ch == '*')
mnsp[j] = 1;
}
}
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
if(mnsp[j] == 0)
cout << check(i,j);
else
cout << '*';
cout << endl;
}
cout << endl;
cin >> m >> n;
count++;
}
return 0;
}[/cpp]
#include<iostream.h>
bool mnsp[101][101];
int m,n;
inline void set(void)
{
memset(mnsp,0,sizeof(mnsp));
}
int check(int a,int b)
{
int t = 0;
if(mnsp[a-1][b-1] == 1)
t++;
if(mnsp[a-1] == 1)
t++;
if(mnsp[a-1][b+1] == 1)
t++;
if(mnsp[a][b-1] == 1)
t++;
if(mnsp[a][b+1] == 1)
t++;
if(mnsp[a+1][b-1] == 1)
t++;
if(mnsp[a+1] == 1)
t++;
if(mnsp[a+1][b+1] == 1)
t++;
return t;
}
int main()
{
int count = 1;
cin >> m >> n;
while(m || n)
{
set();
cout << "Field #" << count << ':' << endl;
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
{
char ch;
cin >> ch;
if(ch == '*')
mnsp[j] = 1;
}
}
for(int i=1;i<=m;++i)
{
for(int j=1;j<=n;++j)
if(mnsp[j] == 0)
cout << check(i,j);
else
cout << '*';
cout << endl;
}
cout << endl;
cin >> m >> n;
count++;
}
return 0;
}[/cpp]
My own quote:
We are here as Adam and Eve were here!
We are here as Adam and Eve were here!
Why WA?
Code: Select all
[c]
#include<stdio.h>
int main()
{
int n,m,c,i,j,ncase=0;
char brd[100][100];
while( scanf("%d %d",&n,&m) == 2 ) {
ncase++;
if( n == 0 && m == 0 )
exit(0);
for( i = 0 ; i < n ; i++ )
for( j = 0 ; j < m ; )
if( (c = getchar()) != '\n' )
if( c == '*' || c == '.' )
brd[i][j++] = c=='.'?'0':c;
for( i = 0 ; i < n ; i++ ) {
for( j = 0 ; j < m ; j++ )
if( brd[i][j] == '*' ){
if( i < n-1 && j > 0 && brd[i+1][j-1] != '*' ) brd[i+1][j-1]++;
if( i < n-1 && j < n-1 && brd[i+1][j+1] != '*' ) brd[i+1][j+1]++;
if( i < n-1 && brd[i+1][j] != '*' ) brd[i+1][j]++;
if( i > 0 && brd[i-1][j] != '*' ) brd[i-1][j]++;
if( i > 0 && j > 0 && brd[i-1][j-1] != '*' ) brd[i-1][j-1]++;
if( i > 0 && j < n-1 && brd[i-1][j+1] != '*' )brd[i-1][j+1]++;
if( j < n-1 && brd[i][j+1] != '*' ) brd[i][j+1]++;
if( j > 0 && brd[i][j-1] != '*' ) brd[i][j-1]++;
}
}
printf("Field #%d:\n",ncase);
for( i = 0 ; i < n ; i++ ) {
for( j = 0 ; j < m ; j++ )
printf("%c",brd[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}
[/c]
Suman.
I still don't know why is my code resulting WA
the sampe input and output is correct, maybe I missed something?
[c]
#include<stdio.h>
#include<string.h>
void main()
{
int n,m;
int i,j;
int k,l;
int ctr;
int num;
char mat[100][100];
num=1;
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
do
{
memset(mat,'0',sizeof(mat));
scanf("%d %d\n",&n,&m);
if(n==0 && m==0)
break;
for(i=0;i<n;i++)
{
gets(mat);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
ctr=0;
if(mat[j]!='*')
{
for(k=i-1;k<=i+1;k++)
{
for(l=j-1;l<=j+1;l++)
{
if(mat[k][l]=='*')
ctr++;
}
}
mat[j]=ctr+'0';
}
}
}
printf("Field #%d:\n",num);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%c",mat[j]);
}
printf("\n");
}
num++;
printf("\n");
}while(n!=0 && m!=0);
/*
fcloseall();
*/
}
[/c]
the sampe input and output is correct, maybe I missed something?
[c]
#include<stdio.h>
#include<string.h>
void main()
{
int n,m;
int i,j;
int k,l;
int ctr;
int num;
char mat[100][100];
num=1;
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
do
{
memset(mat,'0',sizeof(mat));
scanf("%d %d\n",&n,&m);
if(n==0 && m==0)
break;
for(i=0;i<n;i++)
{
gets(mat);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
ctr=0;
if(mat[j]!='*')
{
for(k=i-1;k<=i+1;k++)
{
for(l=j-1;l<=j+1;l++)
{
if(mat[k][l]=='*')
ctr++;
}
}
mat[j]=ctr+'0';
}
}
}
printf("Field #%d:\n",num);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%c",mat[j]);
}
printf("\n");
}
num++;
printf("\n");
}while(n!=0 && m!=0);
/*
fcloseall();
*/
}
[/c]
-
- New poster
- Posts: 18
- Joined: Fri Oct 10, 2003 8:46 am
- Location: Airway Heights
10189 Comile errer?
I keep getting a compile error, but I can compile and run my program with no error, can anyone explain why (I use g++)?
Thank you,
med
PS - I can send the code if anyone wants me to