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

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

Re: 10189 - Minesweeper

Post by Taman »

I think new posters should read the earlier discussions first. If they fail to help you then you should ask for help. I have seen that questions in this forum are being repeated!!! But why will you waste your time waiting for another reply? Just check it out, may be you would be able to get the required information you are looking for on the earlier posts! :D
Taman
New poster
Posts: 32
Joined: Sun Oct 11, 2009 8:59 pm
Location: Southeast University

Re: 10189-Minesweeper (WHY WA)

Post by Taman »

OOOPS!
I've found that there is another thread on the same problem. You should check that thread first. And openning this new thread would not be needed then. . .check that out if you have any more questions. . .
RomanKing
New poster
Posts: 1
Joined: Sun Nov 01, 2009 2:35 pm

Re: 10189-Minesweeper (WHY WA)

Post by RomanKing »

I keep getting Runtime error. Can someone please check.

Code: Select all

#include <stdio.h>
main()
{
  int f=1,ch,j,i,m,n;

  scanf("%d %d ",&m,&n);
  while ((m!=0)&&(n!=0))
  {
    char **st = new char*[m+1];
    for (i=0;i<m+1;i++)
      st[i] = new char[n];

    char **st1 = new char*[m+3];
    for (i=0;i<m+2;i++)
      st1[i] = new char[n+3];


    for (i=0;i<m;i++)
      gets(st[i]);

    for (i=0;i<m+2;i++)
      for(j=0;j<n+2;j++)
        st1[i][j]='0';


    for (i=0;i<m;i++)
      for (j=0;j<n;j++)
        if (st[i][j]=='*')
        {
          st1[i+1][j+1]='*';
          //1
          if(st1[i][j]!='*')
          {
            ch=st1[i][j];
            ch+=1;
            st1[i][j]=ch;
          }
          //2
          if(st1[i][j+1]!='*')
          {
            ch=st1[i][j+1];
            ch+=1;
            st1[i][j+1]=ch;
          }
          //3
          if(st1[i][j+2]!='*')
          {
            ch=st1[i][j+2];
            ch+=1;
            st1[i][j+2]=ch;
          }
          //4
          if(st1[i+1][j]!='*')
          {
            ch=st1[i+1][j];
            ch+=1;
            st1[i+1][j]=ch;
          }
          //5
          if(st1[i+1][j+2]!='*')
          {
            ch=st1[i+1][j+2];
            ch+=1;
            st1[i+1][j+2]=ch;
          }
          //6
          if(st1[i+2][j]!='*')
          {
            ch=st1[i+2][j];
            ch+=1;
            st1[i+2][j]=ch;
          }
          //7
          if(st1[i+2][j+1]!='*')
          {
            ch=st1[i+2][j+1];
            ch+=1;
            st1[i+2][j+1]=ch;
          }
          //8
          if(st1[i+2][j+2]!='*')
          {
            ch=st1[i+2][j+2];
            ch+=1;
            st1[i+2][j+2]=ch;
          }

    }
    printf("Field #%d:\n",f);
    for (i=0;i<m;i++)
    {
      for (j=0;j<n;j++)
        printf("%c",st1[i+1][j+1]);
      printf("\n");
    }
    scanf("%d %d",&m,&n);
    f++;
    printf("\n");

  }
}
behnam.hamidi
New poster
Posts: 2
Joined: Sat Feb 06, 2010 6:53 pm

Re: 10189 - Minesweeper

Post by behnam.hamidi »

hello
i get a WA again and again but idont lnow where my mistake is
please help me
plz mail me =behnam.hamidi1369@gmail.com
this is my code in c++
//soalo khoob bekhoon
#include <iostream>
using namespace std;
int main()
{
int m,n,i,j,counter=0,flag=0;
char a[1000][1000]={'0'};
char ch;
while(cin>>m>>n)
{
if(n<0||n>100||m<0||m>100)continue;
if(n==0&&m==0){flag=1;
return 1;}
counter++;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
// scanf("%c",&a[j]);
cin>>a[j];
}

}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[j]!='*')
a[j]=48;
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[j]=='*')
{
if(a[i+1][j]!='*'&&i!=m-1)
a[i+1][j]++;
if(a[i-1][j]!='*'&&i!=0)
a[i-1][j]++;
if(a[j+1]!='*'&&j!=n-1)
a[j+1]++;
if(a[j-1]!='*'&&j!=0)
a[j-1]++;
if(a[i+1][j-1]!='*'&&j!=0&&i!=m-1)
a[i+1][j-1]++;
if(a[i+1][j+1]!='*'&&i!=m-1&&j!=n-1)
a[i+1][j+1]++;
if(a[i-1][j+1]!='*'&&i!=0&&j!=m-1)
a[i-1][j+1]++;
if(a[i-1][j-1]!='*'&&i!=0&&j!=0)
a[i-1][j-1]++;
}
}
}
cout<<"Field #"<<counter<<":"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[j];
}
// if(i!=m-1)
// cout<<endl;
if(flag!=1)cout<<endl;
}
if(flag!=1)cout<<endl;
}

return 0;
}
help me plz i confused :oops: :oops:
The_Madman
New poster
Posts: 12
Joined: Fri May 23, 2008 10:24 pm

Re: 10189 - Minesweeper

Post by The_Madman »

To : behnam.hamidi

1. remove al && conditions from if statements
2. output a blank line after each field, currently your program output 2 blank lines at end of file.

hope this helps
dejavu_logic
New poster
Posts: 6
Joined: Sat Mar 06, 2010 8:30 am

10189 - Minesweeper

Post by dejavu_logic »

Hello I don't know why I got WA all the time
The output file had the same output though even the number of endline and the number of character there
are exactly the same with the sample output

Code: Select all

#include<iostream>
#include<fstream>

using namespace std;

int main(){
    
    long cols = 0, rows = 0;
    long nThField = 0;
    long mines = 0;
    long checkedRow = 0;
    long checkedCol = 0;
    
    while (cin>>rows>>cols){// get # of row n col
          char field[rows][cols];
          
          if (!rows && !cols) break;
          else if (nThField > 0) cout<<"\n";
          
          for (int i = 0; i < rows; i++){// iterate row
              for (int j = 0; j < cols; j++){// iterate col
                  cin>>field[i][j];
              }
          }
          
          //output the value
          nThField++;
          cout<<"Field #"<<nThField<<":"<<"\n";
          for (int i = 0; i < rows; i++){// iterate row
              for (int j = 0; j < cols; j++){// iterate col
                  if (field[i][j] == '.'){//print the number of mine around
                     // checking for possible mine around in 3x3 area
                     mines = 0;
                     for (int tRow = -1; tRow <= 1; tRow++){
                         for (int tCol = -1; tCol <= 1; tCol++){
                             checkedRow = i + tRow;
                             checkedCol = j + tCol;
                             if (checkedRow < 0 || checkedCol < 0) continue;// if col or row out of bound
                             if (field[i + tRow][j + tCol] == '*'){
                                mines++;
                             }
                         }
                     }
                     cout<<mines;
                  } //end of printing the number of mine
                  else cout<<'*'; //print the mine
              }// end iterating col
              cout<<"\n";
          }// end iterating row
          
    }
    
    return 0;
}
Any help would be very appreciated
mintae71
New poster
Posts: 18
Joined: Tue Jan 19, 2010 10:50 am

Why WA?

Post by mintae71 »

Hello.
First, I'm sorry of my bad english.....
ok, then, lets start.
I don't know why this is WA...
I think because of newline part.
Plz help..........

Code: Select all

Removed after AC
amishera
New poster
Posts: 38
Joined: Sat Dec 27, 2008 10:42 pm

Re: 10189 - Minesweeper

Post by amishera »

This is a rather silly problem and I tried with all possible inputs but still not accepted. I checked with following testcases:

1. minimum
1 1
.
1 1
*

2. maximum
*** upto 100
*** upto 100
repeat until 100

3. for each position I test for mine
4 4
*...
....
....
4 4
.*..
....
....
....

lastly
4 4
....
....
....
...*

4. for each postion I put a '.' and surround it with mines
4 4
.***
****
****
****
4 4
*.**
****
****
****
lastly
4 4
****
****
****
***.

I thought that these are reasonable and representative sets of testcases. But still WA. I am not sure what else could I test for. I would be glad if someone throw some light on further strategy to test this thing.

BTW I incorporated some of the testcases mentioned earlier in the posts and it seemed to pass. I also tested for blank line in-between such as:

Field #51:
****
****
****
*5**

Field #52:
****
****
****
***3

Thanks.
sajnt
New poster
Posts: 2
Joined: Fri Jun 18, 2010 10:03 pm

Re: 10189 - Minesweeper

Post by sajnt »

Code: Select all

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define ST first
#define ND second
#define PB push_back
#define MP make_pair
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<VI> VVI;

char A[105][105];
char ANS[105][105];

char count(int i, int j)
{
	char sum = 0;
	if ( A[i][j] == '*' ) return '*'-'0';
	if ( A[i-1][j] == '*' ) sum++;
	if ( A[i-1][j+1] == '*' ) sum++;
	if ( A[i][j+1] == '*' ) sum++;
	if ( A[i+1][j+1] == '*' ) sum++;
	if ( A[i+1][j] == '*' ) sum++;
	if ( A[i+1][j-1] == '*' ) sum++;
	if ( A[i][j-1] == '*' ) sum++;
	if ( A[i-1][j-1] == '*' ) sum++;
	return sum;
}

int main()
{
	int n, m;
	int z = 1;
	while ( scanf("%d%d",&n,&m) != EOF )
	{
		if ( n == 0 && m == 0 ) break;
		if ( z != 1 ) printf("\n");
		printf("Field #%d: \n", z);
		z++;
		if ( n*m != 0 )
		{
			REP (i, n+2)
				REP (j, m+2)
					A[i][j] = 0;
			FOR (i, 1, n+1)
				scanf("%s",A[i]+1);
			FOR (i, 1, n+1)
				FOR(j, 1, m+1)
					ANS[i][j] = count(i, j);
			FOR (i, 1, n+1)
			{
				FOR(j, 1, m+1)
					printf("%c", ANS[i][j]+'0'); 
				if ( m != 0 ) printf("\n");
			}
		}
	}
	return 0;
}
What is going on? I believe that i don't have any extra lines but I still get PE. Or does ( m != 0 ) printf("\n"); count as a new line?
fzafarani
New poster
Posts: 1
Joined: Mon Jul 19, 2010 10:50 pm

Re: 10189 - Minesweeper

Post by fzafarani »

Can anyone help me why my code gets WA? :|

Code: Select all

#include <iostream>
using namespace std;

int main(void) {
	//	ofstream cout("out.txt");
	int m, n;
	int fl = 1;
	while (cin >> m >> n) {
		char game[120][120];
		char res[120][120];
		if (m == 0 && n == 0)
			break;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++)
				cin >> game[i][j];
		for (int i = 0; i < m; i++)
			for (int j = 0; j < n; j++) {
				if (game[i][j] == '*')
					res[i][j] = '*';
				else {
					int cnt = 0;
					if (j + 1 < n)
						if (game[i][j + 1] == '*')
							cnt++;
					if (i + 1 < m && j + 1 < n)
						if (game[i + 1][j + 1] == '*')
							cnt++;
					if (i + 1 < m)
						if (game[i + 1][j] == '*')
							cnt++;
					if (i + 1 < m && j - 1 >= 0)
						if (game[i + 1][j - 1] == '*')
							cnt++;
					if (j - 1 >= 0)
						if (game[i][j - 1] == '*')
							cnt++;
					if (i - 1 >= 0 && j - 1 >= 0)
						if (game[i - 1][j - 1] == '*')
							cnt++;
					if (i - 1 >= 0)
						if (game[i - 1][j] == '*')
							cnt++;
					if (i - 1 >= 0 && j + 1 < n)
						if (game[i - 1][j + 1] == '*')
							cnt++;
					res[i][j] = cnt + '0';
				}
			}
		if (fl > 1)
			cout << endl << endl;
		cout << "Field #" << fl << ":" << endl;
		fl++;
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++)
				cout << res[i][j];
			if (i != m - 1)
				cout << endl;
		}
	}
	return 0;
}

acoconut
New poster
Posts: 1
Joined: Mon Jul 26, 2010 1:02 pm

Re: 10189 - Minesweeper

Post by acoconut »

Can someone help me? I keep getting wrong answer and I can't figure out why! :-/ Tricky cases to test would be appreciated too.

Code: Select all

#include <iostream>

class cas{
	bool mine;
	public:
	cas ();
	bool get_mine();
	void set_mine(bool);
};

cas::cas (){
	mine = false;
}

bool cas::get_mine (){
	return mine;
}

void cas::set_mine (bool b){
	mine = b;
}

int main(){
	unsigned int fields=0;
	int n = 0;
	int m = 0;
	char a;
	bool b;
	unsigned int cont = 0;
	std::cin>>n;
	std::cin>>m;
	while (n !=0 || m != 0){
		fields++;
		std::cout<<"Field #"<< fields<< std::endl;
		cas matrix[n][m];

		for (int i=0; i <n; i++){
			for (int j = 0; j < m; j++){
				std::cin >> a;
				if (a == '*'){
					matrix[i][j].set_mine(true);
				}
			}
		}
		
		for (int i=0; i<n; i++){
			for (int j=0; j<m; j++){
				if (matrix[i][j].get_mine()){
					std::cout<<"*";
				}else{
					//check
					cont = 0;
					if (i == 0 && j==0 && i == n-1 && j == m-1) {
						cont = 0;
					}else if (i == 0 && j == 0){
						//esquina arriba izquierda
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i+1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j+1].get_mine()){
							cont++;
						}
					}else if (i == 0 && j == m-1){
						//esquina arriba derecha
						if (matrix[i+1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j-1].get_mine()) {
							cont++;
						}
						if (matrix[i][j-1].get_mine()){
							cont++;
						}
					}else if (i == n-1 && j == 0){
						//esquina abajo izquierda
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i-1][j].get_mine()) {
							cont++;
						}
						if (matrix[i-1][j+1].get_mine()){
							cont++;
						}
					}else if (i == n-1 && j == m-1){
						//esquina abajo derecha
						if (matrix[i-1][j].get_mine()){
							cont++;
						}
						if (matrix[i-1][j-1].get_mine()) {
							cont++;
						}
						if (matrix[i][j-1].get_mine()){
							cont++;
						}
					}else if (i == 0 && j != 0 && j != m-1){
						//linea de arriba, pero no esquinas
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i][j-1].get_mine()) {
							cont++;
						}
						if (matrix[i+1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j-1].get_mine()){
							cont++;
						}
						if (matrix[i+1][j+1].get_mine()){
							cont++;
						}
					}else if (i == n-1 && j != 0 && j != m-1){
						//linea de abajo, pero no esquinas
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i][j-1].get_mine()) {
							cont++;
						}
						if (matrix[i-1][j].get_mine()){
							cont++;
						}
						if (matrix[i-1][j-1].get_mine()){
							cont++;
						}
						if (matrix[i-1][j+1].get_mine()){
							cont++;
						}
					}else if (i != 0 && i != n-1 && j == 0){
						//columna izquierda pero no esquinas
						if (matrix[i-1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j].get_mine()) {
							cont++;
						}
						if (matrix[i+1][j+1].get_mine()){
							cont++;
						}
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i-1][j+1].get_mine()){
							cont++;
						}
					}else if (i != 0 && i != n-1 && j == m-1){
						//columna derecha pero no esquinas
						if (matrix[i-1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j].get_mine()) {
							cont++;
						}
						if (matrix[i+1][j-1].get_mine()){
							cont++;
						}
						if (matrix[i][j-1].get_mine()){
							cont++;
						}
						if (matrix[i-1][j-1].get_mine()){
							cont++;
						}
					}else{
						//celda central
						if (matrix[i-1][j-1].get_mine()){
							cont++;
						}
						if (matrix[i-1][j].get_mine()) {
							cont++;
						}
						if (matrix[i-1][j+1].get_mine()){
							cont++;
						}
						if (matrix[i][j-1].get_mine()){
							cont++;
						}
						if (matrix[i][j+1].get_mine()){
							cont++;
						}
						if (matrix[i+1][j-1].get_mine()){
							cont++;
						}
						if (matrix[i+1][j].get_mine()){
							cont++;
						}
						if (matrix[i+1][j+1].get_mine()){
							cont++;
						}
					}
					std::cout<<cont;
				}	
			}
			std::cout<<std::endl;
		}
	std::cout<<std::endl;
	std::cin>>n;
	std::cin>>m;

	}
}
ahmed1098
New poster
Posts: 1
Joined: Fri Jul 30, 2010 3:16 pm

Need Help in Minesweeper why i get WA

Post by ahmed1098 »

hi all i am new in java and in algorithms and now i get WA in Minesweeper here is my code can any one tell me in which test case it will give a WA

Code: Select all

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



public class Main {

   
    public static int n_bombs(int x, int y , char [][]map,int n,int m)
    {
        int count=0;
      
if(x-1>=0)
{
       if(map[x-1][y]== '*') count++;
}
       if(x+1<n)
       {
       if(map[x+1][y]=='*') count++;
       }
       if(y+1<m)
       {
       if(map[x][y+1]=='*') count++;
       }
       if(y-1>=0)
       {
       if(map[x][y-1]=='*') count++;
       }
       if(x+1<n && y+1<m)
       {
       if(map[x+1][y+1]=='*') count++;
       }
       if(x-1>=0 && y-1>=0)
       {
       if(map[x-1][y-1]=='*') count++;
       }
       if(x+1<n && y-1>=0)
       {
       if(map[x+1][y-1]=='*') count++;
       }
       if(x-1>=0 && y+1<m)
       {
       if(map[x-1][y+1]=='*') count++;
       }
       return count;
        }

    public static void main(String[] args) throws IOException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String line;int a=0;
		while ((line = reader.readLine()).equals("0 0")!=true) {
a++;
    String []data=line.split(" ");
  int n=Integer.parseInt(data[0]);
  int m =Integer.parseInt(data[1]);
 char [][]map=new char[n][m];
  for(int k=0;k<n;k++)
  {
      line=reader.readLine();
      char []r=new char[m];
      line.getChars(0, m, r, 0);
      
      for(int o=0;o<m;o++)
          map[k][o]=r[o];
  }
 System.out.println("Field #"+a+":");
      for(int y=0;y<n;y++)
      {
          for(int x=0;x<m;x++)
          {
              if(map[y][x]=='*')
              System.out.print('*');
              else
              {
                  System.out.print(n_bombs(y,x,map,n,m));
              }

          }
          System.out.println();
          }
 System.out.println();
  }
  }

}       


hosnayen
New poster
Posts: 4
Joined: Mon Dec 28, 2009 8:41 pm

Re: 10189 - Minesweeper

Post by hosnayen »

i m getting wrong answer :( . can any1 help me :( , i dont understand why :(. here is my code :-

/*
Author : Hosnayen Alam Siddiquee.
University of Science & Technology Chittagong (USTC)
CSE- 11th Batch
E-mail: hosnayen_alam@yahoo.com
Bangladesh Date: 30/07/2010
*/

#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

int main()
{
int i,j,n,m,count=0,kase=0;

//freopen("10189_in.txt","r",stdin);

while(cin >> n >>m)
{
if(n==0 && m==0) break;

char r[110][110];
int sum[110][110]={0};

if(kase++){
cout<<endl;
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{

cin>> r [j] ;
}
}


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (r [j]=='*')
{
sum [j+1]++;
sum [i+1] [j+1]++;
sum [i+1] [j]++;
sum [i+1] [j-1]++;
sum [j-1]++;
sum [i-1] [j-1]++;
sum [i-1] [j]++;
sum [i-1] [j+1]++;
}

}
}

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

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(r [j] == '*')
{
cout<< "*";
}
else
cout<<sum[j];

}

cout<<endl;
}
}



return 0;
}
hosnayen
New poster
Posts: 4
Joined: Mon Dec 28, 2009 8:41 pm

Re: 10189 - Minesweeper

Post by hosnayen »

i m getting wrong answer :( . can any1 help me :( , i dont understand why :(. here is my code :-

/*
Author : Hosnayen Alam Siddiquee.
University of Science & Technology Chittagong (USTC)
CSE- 11th Batch
E-mail: hosnayen_alam@yahoo.com
Bangladesh Date: 30/07/2010
*/

#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

int main()
{
int i,j,n,m,count=0,kase=0;

//freopen("10189_in.txt","r",stdin);

while(cin >> n >>m)
{
if(n==0 && m==0) break;

char r[110][110];
int sum[110][110]={0};

if(kase++){
cout<<endl;
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{

cin>> r [j] ;
}
}


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (r [j]=='*')
{
sum [j+1]++;
sum [i+1] [j+1]++;
sum [i+1] [j]++;
sum [i+1] [j-1]++;
sum [j-1]++;
sum [i-1] [j-1]++;
sum [i-1] [j]++;
sum [i-1] [j+1]++;
}

}
}

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

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(r [j] == '*')
{
cout<< "*";
}
else
cout<<sum[j];

}

cout<<endl;
}
}



return 0;
}
jol
New poster
Posts: 1
Joined: Sat Sep 18, 2010 7:17 pm

Re: Need Help in Minesweeper why i get WA

Post by jol »

ok guys..

i think you must check the statement in question "There must
be an empty line between field outputs".

if dont have submission again, you don't put empty line("\n");
:)

* in first my attempt i got WA, but after i correction i get AC..
Post Reply

Return to “Volume 101 (10100-10199)”