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

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
morcef
New poster
Posts: 2
Joined: Wed Feb 11, 2015 8:27 pm

Re: 10189 - Minesweeper

Post by morcef »

still returning Wrong Answer :/

@edit
It's not Presentation Error, but all the test cases worked
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Post your updated code.
Missing or extra newline chars will usually result in WA, not PE.
Check input and AC output for thousands of problems on uDebug!
mnjpomme
New poster
Posts: 1
Joined: Tue Feb 17, 2015 2:05 am

I am experiencing WA for this code

Post by mnjpomme »

Could you please point out what kind of consideration is needed for this minesweeper case ?

Code: Select all

#include <stdio.h>

#define MAX_NUM 100
#define STAR 777
int answer[100][100];
int subset[8][2] = {
	{-1, -1},
	{-1, 0},
	{-1, 1},
	{0, -1},
	{0, 1},
	{1, -1},
	{1, 0},
	{1, 1}
};

int N, M;

void increase_adjacent_field(int n, int m)
{
	int i;
	int _n, _m;

	for (i = 0; i < 8; i++) {
		_n = n + subset[i][0];
		_m = m + subset[i][1];
		if (_n >= 0 && _n < N && _m >= 0 && _m < M)
			if (answer[_n][_m] != STAR)
				answer[_n][_m] += 1;
	}
}

int main(void)
{
	int i, j;
	char ch;
	int count = 1;

	while (scanf("%d %d", &N, &M) != EOF) {
		if (!N && !M)
			break;

		for (i = 0; i < N; i++) {
			j = 0;
			while(j < M) {
				scanf("%c", &ch);
				if (ch == '*')
					answer[i][j++] = STAR;
				else if (ch == '.')
					answer[i][j++] = 0;
			}
		}

		for (i = 0; i < N; i++)
			for (j = 0; j < N; j++)
				if (answer[i][j] == STAR)
					increase_adjacent_field(i, j);


		printf("Field #%d:\n", count);

		for (i = 0; i < N; i++) {
			for (j = 0; j < M; j++) {
				if (answer[i][j] == STAR)
					printf("*");
				else
					printf("%d", answer[i][j]);
			}
			printf("\n");
		}
		count++;
		printf("\n");
	}
	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 »

brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
ormaza
New poster
Posts: 2
Joined: Fri Apr 03, 2015 10:07 pm

10189 - Minesweeper - WA

Post by ormaza »

Please, someone can see what's wrong with my code. It passes in all tests who I've done on UDebug, but the answer is always WA.

Code: Select all

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstring>

using namespace std;

int main()
{
    int m,n,field=1;
    char checa[10000];
    cin>>checa;
    m=atoi(checa);
    memset(checa,NULL,10000);
    int cont=0,maximo=0;
    while(cin>>n)
    {

        if(n!=0 || m!=0)
        {
            int tam=strlen(checa);
            maximo=tam;
            do{

                    if(m==0) {cout<<"Field #"<<field<<":"<<endl; field++;}
                    else if(n==0)
                    {
                        cout<<"Field #"<<field<<":"<<endl;
                        field++;
                        for(int i=0;i<m;i++)
                            cout<<endl;

                    }
            else{

            char campo [105][105];

            //preenche o campo
            for(int l=0;l<m;l++)
            {
                for(int c=0;c<n;c++)
                {
                    campo[l][c]='.';
                }
            }

            for(int l=0;l<m;l++)
            {
                for(int c=0;c<n;c++)
                {
                        if(tam>0){
                            campo[l][c]=checa[cont++];
                            tam--;
                            if(cont==maximo) cont=0;
                        }
                        else{
                            cin>>campo[l][c];
                            }
                }

            }

            cout<<"Field #"<<field<<":"<<endl;
            field++;

            //função preenche (nao se stressar :p)

            for(int l=0;l<m;l++)
            {
                for(int c=0;c<n;c++)
                {
                    if(campo[l][c]!='*')
                    {
                        //verificando casas adjacentes
                        int minas=0;
                        if(l!=0)
                        {
                            if((campo[l-1][c])=='*') minas++;
                            if(c<n-1)
                            {
                                if((campo[l-1][c+1])=='*') minas++;
                            }
                            if(c>0)
                            {
                                if((campo[l-1][c-1])=='*') minas++;
                            }
                        }
                        if(l<m-1)
                        {
                            if((campo[l+1][c])=='*') minas++;
                            if(c<n-1)
                            {
                                if((campo[l+1][c+1])=='*') minas++;
                            }
                            if(c>0)
                            {
                                if((campo[l+1][c-1])=='*') minas++;
                            }
                        }
                        if(c>0)
                        {
                           if((campo[l][c-1])=='*') minas++;
                        }
                        if(c<n-1)
                        {
                            if((campo[l][c+1])=='*') minas++;
                        }
                        cout<<minas;
                    }
                    else
                    cout<<campo[l][c];
                }
                cout<<endl;
    }

            //acabou!!!!!!!!!!!!

        cout<<endl;
        }
        if(tam==0){
        cin>>checa;
        tam=strlen(checa);
        if(checa[0]=='0')
        {
            tam=0;
            m=0;
            memset(checa,NULL,10000);
        }
        }

        if(atoi(checa)!=0)
        {
            m=atoi(checa);
            memset(checa,NULL,10000);
            tam=strlen(checa);
            break;
        }
            }while(tam>0);
        }
        else
        {
            break;
        }

    }

    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 »

brianfry713 wrote:
brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
ormaza
New poster
Posts: 2
Joined: Fri Apr 03, 2015 10:07 pm

Re: 10189 - Minesweeper

Post by ormaza »

Image
This is not the problem. Has an empty line between field outputs and doesn't has a blank line at the end.
moar
New poster
Posts: 1
Joined: Mon Aug 10, 2015 5:45 pm

Re: 10189 - Minesweeper

Post by moar »

Hi i can't find what I am doing in my java code for this problem. I found out that it only works in eclipse where i enter the grid size then press enter then paste the fields then repeat again for all test cases. That is:
4 4
Then press enter , then paste the rest :
*...
....
.*..
....
However if i paste a single test case all at the same time nothing happens , and if i do it on ideone i get run-time error.

Here is my code :

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	static int field=0;
	
	public static void main(String [] args) throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer s=new StringTokenizer(bf.readLine());
		int b=Integer.parseInt(s.nextToken()), a=Integer.parseInt(s.nextToken());
		 
		
		while(b!=0 && a!=0){	
		field++;
		mine(b,a);
		s=new StringTokenizer(bf.readLine());
		b=Integer.parseInt(s.nextToken()); a=Integer.parseInt(s.nextToken());
		}
	}
	
	public static void mine(int a,int b) throws IOException{
		
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		boolean array_first=false,array_last=false,string_first=false,string_last=false;
		
		String []x=new String[a];
		
		for(int i=0;i<a;i++)
			x[i]=bf.readLine();
		
		System.out.println("Field #"+field+":");
		
		
		for(int i=0;i<a;i++){
			array_first=(i==0)?true:false;
		    array_last=(i==a-1)?true:false;
		    
				for(int j=0; j<b; j++){
					string_first=(j==0)?true:false;
			    	string_last=(j==b-1)?true:false;
					int counter=0;
						
						if(x[i].charAt(j)!='*'){
					
						if(!string_first){
							if(x[i].charAt(j-1)=='*')
							counter++;
							if(!array_first && x[i-1].charAt(j-1)=='*')
								counter++;
							
							if(!array_last && x[i+1].charAt(j-1)=='*')
								counter++;
					
						}
						if(!string_last ){
							if(x[i].charAt(j+1)=='*')
								counter++;
								if(!array_first && x[i-1].charAt(j+1)=='*')
									counter++;
								if(!array_last && x[i+1].charAt(j+1)=='*')
									counter++;
						}
						if(!array_first && x[i-1].charAt(j)=='*')
							counter++;
						
						if(!array_last && x[i+1].charAt(j)=='*')
							counter++;
						
						System.out.print(counter);
						}
						
						else
							System.out.print('*');
							
							
						
							
							
							
				}
				System.out.println();
		}
		System.out.println();
	}

}
Last edited by brianfry713 on Wed Aug 19, 2015 8:50 pm, edited 1 time in total.
Reason: Added code block
lolbroken
New poster
Posts: 1
Joined: Mon Aug 31, 2015 3:58 am

Re: 10189 - Minesweeper

Post by lolbroken »

Hey I have been working on this for a while and I cant get it to work. every case that I can think of is represented and the output is always correct and appears formatted correctly as I can see it(i.e. no extra lines etc.) but it is still getting a WA when I submit it. Any help would be greatly appreciated. C++

Code: Select all

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
   int h, w, num, count = 0;
   
   while(cin >> h >> w)
   {
      
      if ((h == 0) && (w == 0))
      {
         return 0;
      }
      if (count >= 1)
         cout << endl;
      count++;
      char board[h][w];
      char digits[] = {'0','1','2','3','4','5','6','7','8','9'};
      for(int a = 0; a < h; a++)
      {
         for(int b = 0; b < w; b++)
         {
            cin >> board[a][b];
         }
      }
      
      for(int a = 0; a < h; a++)
      {
         for(int b = 0; b < w; b++)
         {
            num = 0;
            if ((h ==1) && (w == 1) && (board[a][b] == '.'))
            {
                  board[a][b] = '0';
            }

            else if (h == 1)
            {
               if (b == 0 && board[a][b] == '.')
               {
                  if (board[a][b+1] == '*')
                     num++;
                  board[a][b] = digits[num];
               }
               else if (b == w-1 && board[a][b] == '.')
               {
                  if (board[a][b-1] == '*')
                     num++;
                  board[a][b] = digits[num];
               }
               else if (board[a][b] == '.')
               {
                  if (board[a][b-1] == '*')
                     num++;
                  if (board[a][b+1] == '*')
                     num++;
                  board[a][b] = digits[num];
               }
               
            }

            else if (w == 1)
            {
               if (a == 0 && board[a][b] == '.')
               {
                  if (board[a+1][b] == '*')
                     num++;
                  board[a][b] = digits[num];
               }
               else if (a == h-1 && board[a][b] == '.')
               {
                  if (board[a-1][b] == '*')
                     num++;
                  board[a][b] = digits[num];
               }
               else if (board[a][b] == '.')
               {
                  if (board[a-1][b] == '*')
                     num++;
                  if (board[a+1][b] == '*')
                     num++; 
                  board[a][b] = digits[num];
               }
            }
            else if ((a == 0) && (b == 0) && (board[a][b] == '.'))
            {
               if (board[a][b+1] == '*')
                  num++;
               if (board[a+1][b] == '*')
                  num++;
               if (board[a+1][b+1] == '*')
                  num++;
               board[a][b] = digits[num];
            }

            else if ((a == 0) && (b == w-1) && (board[a][b] == '.'))
            {
               if (board[a][b-1] == '*')
                  num++;
               if (board[a+1][b] == '*')
                  num++;
               if (board[a+1][b-1] == '*')
                  num++;
               board[a][b] = digits[num];
            }

            else if (((a == 0) && ((b != 0) || (b != w-1))) && (board[a][b] == '.'))
            {
               if (board[a][b-1] == '*')
                  num++;
               if (board[a+1][b] == '*')
                  num++;
               if (board[a][b+1] == '*')
                  num++;
               if (board[a+1][b-1] == '*')
                  num++;
               if (board[a+1][b+1] == '*')
                  num++;
               board[a][b] = digits[num];
            }
            
            else if ((a == h-1) && (b == 0) && (board[a][b] == '.'))
            {
               if (board[a-1][b] == '*')
                  num++;
               if (board[a][b+1] == '*')
                  num++;
               if (board[a-1][b+1] == '*')
                  num++;
               board[a][b] = digits[num];
            }

            else if ((a == h-1) && (b == w-1) && (board[a][b] == '.'))
            {
               if (board[a][b-1] == '*')
                  num++;
               if (board[a-1][b] == '*')
                  num++;
               if (board[-1][b-1] == '*')
                  num++;
               board[a][b] = digits[num];
            }

            else if (((a == h-1) && ((b != 0) || (b != w-1))) && (board[a][b] == '.'))
            {
               if (board[a][b-1] == '*')
                  num++;
               if (board[a-1][b] == '*')
                  num++;
               if (board[a][b+1] == '*')
                  num++;
               if (board[a-1][b+1] == '*')
                  num++;
               if (board[a-1][b-1] == '*')
                  num++;
               board[a][b] = digits[num];
            }
            
            else if (((b == 0) && ((a != 0) || (a != h-1))) && (board[a][b] == '.'))
            {
               if (board[a-1][b] == '*')
                     num++;
               if (board[a][b+1] == '*')
                     num++;
               if (board[a+1][b] == '*')
                     num++;
               if (board[a-1][b+1] == '*')
                     num++;
               if (board[a+1][b+1] == '*')
                     num++;
               board[a][b] = digits[num];
            }
            
            else if (((b == w-1) && ((a != 0) || (a != h-1))) && (board[a][b] == '.'))
            {
               if (board[a-1][b] == '*')
                     num++;
               if (board[a-1][b-1] == '*')
                     num++;
               if (board[a][b-1] == '*')
                     num++;
               if (board[a+1][b-1] == '*')
                     num++;
               if (board[a+1][b] == '*')
                     num++;
               board[a][b] = digits[num];
            }
            
            else if (board[a][b] == '.')
            {
               if (board[a-1][b-1] == '*')
                  num++;
               if (board[a-1][b] == '*')
                  num++;
               if (board[a-1][b+1] == '*')
                  num++;
               if (board[a][b-1] == '*')
                  num++;
               if (board[a][b+1] == '*')
                  num++;
               if (board[a+1][b-1] == '*')
                  num++;
               if (board[a+1][b] == '*')
                  num++;
               if (board[a+1][b+1] == '*')
                  num++;
               board[a][b] = digits[num];
            }
         }
      }
            
      cout << "Field #" << count << ":\n";
      for(int a = 0; a < h; a++)
      {
         for(int b = 0; b < w; b++)
         {
            cout << board[a][b];
         }
         cout << endl;
      }
   }
   return 0;
}
0x539
New poster
Posts: 1
Joined: Tue Oct 27, 2015 7:42 pm

Re: 10189 - Minesweeper

Post by 0x539 »

*********************JAVA*********************
Same problem as many, code works, even with the critical input.
Like brianfry713 said to many others, there might be a newline missing or one too much but I cannot find it.
It seems right to the point. There should be no newline after '0' '0' input and there should be a newline between the input.
Hopefully someone could help me get this code accepted...
Thanks in advance.



Well nevermind, we found the problem and got accepted.
Like the rest it was a newline problem, which got solved with a simple if condition.
rafi4203
New poster
Posts: 1
Joined: Sun Nov 29, 2015 5:27 pm

Re: 10189 - Minesweeper

Post by rafi4203 »

why am i getting WA can anyone help me...?? :(

Code: Select all

#include<bits/stdc++.h>
int main()
{
    char n[110][110],li[110] ;
    int m[110][110],a,b,i,j=0,k=0;
    while(1)
    {
        k++;
    scanf("%d %d",&a,&b);
    if(a==0&&b==0)
        break;
          for(i=0;i<a;i++)
        {
            for(j=0;j<b;j++)
            {
                n[i][j]='\0';
                m[i][j]=0;
            }
        }

    for(i=0;i<a;i++)
    {
           scanf("%s",li);
            for(j=0;j<b;j++)
            {
                n[i][j]=li[j];
            }
    }
        for(i=0;i<a;i++)
        {
            for(j=0;j<b;j++)
            {
                if(n[i][j]=='*')
                {
                    m[i][j]=9;
                    if(j-1>=0&&m[i][j-1]!=9)
                    m[i][j-1]+=1;
                    if(j+1<b&&m[i][j+1]!=9)
                    m[i][j+1]+=1;
                    if(i-1>=0&&m[i-1][j]!=9)
                    m[i-1][j]+=1;
                    if(i+1<a&&m[i+1][j]!=9)
                    m[i+1][j]+=1;
                    if(i-1>=0&&j-1>=0&&m[i-1][j-1]!=9)
                    m[i-1][j-1]+=1;
                    if(i+1<a&&j+1<b&&m[i+1][j+1]!=9)
                    m[i+1][j+1]+=1;
                    if(i-1>=0&&j+1<b&&m[i-1][j+1]!=9)
                    m[i-1][j+1]+=1;
                    if(i+1<a&&j-1>=0&&m[i+1][j-1]!=9)
                    m[i+1][j-1]+=1;
                }
            }
        }
        if(k>1)
         printf("\n\n");
        printf("Field #%d:\n",k);

        for(i=0;i<a;i++)
        {
            for(j=0;j<b;j++)
            {
                if(m[i][j]==9)
                    printf("*");
                else
                printf("%d",m[i][j]);
            }
            if(i!=a-1)
            printf("\n");

        }
    }
    return 0;
}
mozdaher.aq
New poster
Posts: 1
Joined: Mon Aug 08, 2016 12:44 pm

Re: 10189 - Minesweeper Wrong Answer

Post by mozdaher.aq »

I wrote this code but the answer is wrong any suggestions ????

Code: Select all

#include <stdio.h>

int compute(char st[1000][1000], int, int, int, int);

int main()
{
    char st[1000][1000], linebreak;
    int f_no, m, n, i, j;

/*    freopen("input.txt", "r", stdin);*/

    for(scanf(" %d %d", &m, &n), f_no=0; m!=0 && n!=0; scanf(" %d %d", &m, &n), f_no++){

        for(i=0; i<m; i++){
            for(j=0; j<n; j++) scanf("%c", &st[i][j]);
        scanf("%c", &linebreak);
        }
        printf("Field #%d:\n", f_no+1);
        for(i=0; i<m; i++){
            for(j=0; j<n; j++) {
                    if(st[i][j]=='*') printf("%c", st[i][j]);
                    else printf("%d", compute(st, i, j, m, n));
            }
        printf("\n");
        }

    }

    return 0;
}

int compute(char st[1000][1000], int i, int j, int m, int n)
{
    int count=0, ineg=0, ipos=0, jneg=0, jpos=0;
    if(i-1>=0) ineg=1;
    if(i+1<m) ipos=1;
    if(j-1>=0) jneg=1;
    if(j+1<n) jpos=1;

    if(ineg && jneg && st[i-1][j-1]=='*') count++;
    if(ineg && st[i-1][j]=='*') count++;
    if(ineg && jpos && st[i-1][j+1]=='*') count++;

    if(jneg && st[i][j-1]=='*') count++;
    if(st[i][j]=='*') count++;
    if(jpos && st[i][j+1]=='*') count++;

    if(ipos && jneg && st[i+1][j-1]=='*') count++;
    if(ipos && st[i+1][j]=='*') count++;
    if(ipos && jpos && st[i+1][j+1]=='*') count++;

    return count;

}
Majedul
New poster
Posts: 1
Joined: Mon Aug 29, 2016 1:10 pm

Re: 10189 - Minesweeper

Post by Majedul »

why i get wrong answer ..
my code:::

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,f;

int cnt=0;
while(cin>>n>>m)
{
char a[100][100];
int b[100][100]= {0};

if(n==0 && m==0)
break;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
cin>>a[j];


for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[j]=='*')
{
b[j+1]++;
b[i+1][j+1]++;
b[i+1][j]++;
b[i+1][j-1]++;

b[j-1]++;
b[i-1][j-1]++;
b[i-1][j]++;
b[i-1][j+1]++;

}

}
}
cnt++;
if(f==1)
cout<<endl;

f=1;

cout<<"Field #"<<cnt<<":"<<endl;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(a[j]=='*')
cout<<a[j];
else
cout<<b[j];

}
cout<<endl;
}

}

return 0;
}
Post Reply

Return to “Volume 101 (10100-10199)”