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

fkrafi
New poster
Posts: 13
Joined: Wed Sep 15, 2010 1:36 pm

Re: 10189 - Minesweeper

Post by fkrafi »

WHy WA????

Code: Select all

Solved...
Last edited by fkrafi on Tue Mar 01, 2011 2:44 pm, edited 1 time in total.
harry_falcon
New poster
Posts: 1
Joined: Wed Oct 27, 2010 3:35 am

Re: 10189 - Minesweeper

Post by harry_falcon »

can someone help me?
I got WA using this code:

#include <iostream>

using namespace std;

char field[102][102];

int scanArea(int m, int n){
int count = 0;
for(int i = m-1; i <= m+1; i++){
for(int j = n-1; j <= n+1; j++){
if(field[j] == '*') count++;
}
}
return count;
}

int main()
{
int m = 1;
int n = 1;
int count = 0;
int i;
int j;
while(cin >> m >> n){
if(m > 0 && m <= 100 && n > 0 && n <= 100)
{
i=1;
while(i <= m){
j=1;
while(j <= n){
cin >> field[j];
j++;
}i++;
}count++;
cout << "Field #" << count << ":" << endl;
i=1;
while(i <= m){
j=1;
while(j <= n){
if(field[j] == '.') cout << scanArea(i,j);
else cout << '*';
j++;
}i++;
cout << endl;
}
}
}return 0;
}
mody_swd
New poster
Posts: 1
Joined: Sun Oct 31, 2010 3:03 pm

10189 Minesweeper RE

Post by mody_swd »

could any one know why my code verdicts RE: while its optimized for fast execution: ??!!!

#include<iterator>
#include<iostream>
#include<vector>
using namespace std;
struct dim{int x,y;}obj;
int Field_counter=1;
vector<dim> Mines;
int main()
{
int r=-1,c=-1;
while(r!=0&&c!=0 && scanf("%i %i",&r,&c)!=EOF)
{
if(r==0||c==0)break;
if(r<0||r>100||c<0||c>100) return 0;
int **ptr;
char tmp;
ptr=new int *[r+1];
for(int i=0;i<=r+1;i++)
ptr=new int[c+1];
for(int s=0; s<=r; s++)
for(int m=0;m<=c;m++)
{
ptr[s][m]=0;
}
for(int k=1;k<=r;k++)
for(int h=1;h<=c;h++)
{

cin>>tmp;
if(tmp=='*')
{
obj.x=k;
obj.y=h;
Mines.push_back(obj);
ptr[k][h]=tmp;
}
}


int dim_x=0,dim_y=0;
vector<dim>::iterator Itr;
for(Itr=Mines.begin(); Itr != Mines.end();Itr++)
{

dim_x = *&Itr->x;
dim_y = *&Itr->y;

(ptr[dim_x+1][dim_y]=='*'? true:ptr[dim_x+1][dim_y]++);

(ptr[dim_x-1][dim_y]=='*'? true:ptr[dim_x-1][dim_y]++);

(ptr[dim_x+1][dim_y+1]=='*'? true:ptr[dim_x+1][dim_y+1]++);

(ptr[dim_x+1][dim_y-1]=='*'? true:ptr[dim_x+1][dim_y-1]++);

(ptr[dim_x][dim_y-1]=='*'? true:ptr[dim_x][dim_y-1]++);

(ptr[dim_x][dim_y+1]=='*'? true:ptr[dim_x][dim_y+1]++);

(ptr[dim_x-1][dim_y-1]=='*'? true:ptr[dim_x-1][dim_y-1]++);

(ptr[dim_x-1][dim_y+1]=='*'? true:ptr[dim_x-1][dim_y+1]++);

}
cout<<"Field #"<<Field_counter<<":\n";
for(int row=1;row<=r;row++)
{
for(int col=1;col<=c;col++)
{
if(ptr[row][col]=='*')
cout<<'*';
else
cout<<ptr[row][col];
}
cout<<"\n";
}
cout<<"\n";

Mines.clear();
++Field_counter;
}
return 0;
}
// i had tested my code and working fine with many cases .
:o
anjaneyanjan7
New poster
Posts: 2
Joined: Tue Nov 02, 2010 8:33 pm

Minesweeper --problem 10189 WA

Post by anjaneyanjan7 »

Code: Select all

#include <iostream>

using namespace std;

int main()
{
	int a,b,i,j,k=0;
	cin >> a >> b;
	char arr[104][104];

	while(a!=0 && b!=0){
		k++;
		
		for(i=1;i<=a;i++){
			for(j=1;j<=b;j++){
				cin >> arr[i][j];
				if(arr[i][j]!='*')
					arr[i][j]='0';
			}
		}
		for(i=1;i<=a;i++){
			for(j=1;j<=b;j++){
				if(arr[i][j] == '*'){
					 
					if(arr[i+1][j] != '*')
					arr[i+1][j]++;

					 if(arr[i-1][j] != '*')
					arr[i-1][j]++;

					  if(arr[i+1][j+1] != '*')
					arr[i+1][j+1]++;

					   if(arr[i-1][j-1] != '*')
					arr[i-1][j-1]++;

					    if(arr[i][j+1] != '*')
					arr[i][j+1]++;

					     if(arr[i][j-1] != '*')
					arr[i][j-1]++;

					      if(arr[i-1][j+1] != '*')
					arr[i-1][j+1]++;

					       if(arr[i+1][j-1] != '*')
					arr[i+1][j-1]++;
				}
			}
		}
		cout << "Field #" << k <<":"<< endl;
		for(i=1;i<=a;i++){
			for(j=1;j<=b;j++){
				
				cout << arr[i][j] ;
			}
			cout << endl;
		}
		cout << endl;
		cin >> a >> b;
	}
}


kindly let me know why am I getting WA....I suppose my code is doing fine....
live_lie
New poster
Posts: 19
Joined: Mon Nov 29, 2010 11:50 pm

Re: 10189 - Minesweeper

Post by live_lie »

i got AC. you have print a single blank line after all output. but for 0 0 make sure that your program print no blank line.
and be carefull for low inputs. like 1 1 ,2 2or 3 3.
if you chk by loop .be sure that index never over load the value 0f n or m.
ayah.ehab
New poster
Posts: 1
Joined: Sat Jan 08, 2011 6:41 pm

Do I have to print all the output at the end??

Post by ayah.ehab »

I'm new to this, and don't know what's the system. I'm getting WA on minesweeper, and i'm wondering
if the problem is that I print the result of each field after processing it rather than processing all the
input and printing it all at the end. is this ok?
leonardoferrari
New poster
Posts: 5
Joined: Mon Mar 14, 2011 4:59 am

Re: 10189 - Minesweeper

Post by leonardoferrari »

Keep getting WA, don't know what to do.
My Outputs are all right !

Code: Select all

nevermind, got AC.

Thanks in advance !

P.S -> when doing a counter to the field, dont forget to do and endl only at the first output. Aka if(counter) cout << endl;
drahar
New poster
Posts: 1
Joined: Sun Mar 20, 2011 6:26 pm

Re: 10189 - Minesweeper

Post by drahar »

Hi, guys.
I can't figure out what i'm doing wrong. I formatted the output as it should be - one line between fields, no new line after the last field.
In all my test everything looks okay, but i still get WA. I would really appreciate any help :) Here's my code:

Code: Select all

#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void updateField(int **mineField, int posY, int posX, int rows, int columns){

    mineField[posY][posX] = -9;
    //North
    if((posY - 1) >= 0)
        mineField[posY-1][posX]++;

    //South
    if((posY + 1) < rows)
        mineField[posY + 1][posX]++;

    //West
    if((posX - 1) >= 0)
        mineField[posY][posX - 1]++;

    //East
    if((posX + 1) < columns)
        mineField[posY][posX + 1]++;

    //NorthWest
    if(((posY - 1) >=0) && ((posX - 1) >= 0))
        mineField[posY - 1][posX - 1]++;

    //SouthWest
    if(((posY + 1) < rows) && ((posX - 1) >= 0))
        mineField[posY + 1][posX - 1]++;

    //NorthEast
    if(((posY - 1) >=0) && ((posX + 1) < columns))
        mineField[posY - 1][posX + 1]++;

    //SouthEast
    if(((posY + 1) < rows) && ((posX + 1) < columns))
        mineField[posY + 1][posX + 1]++;
}


int main(int argc, char** argv) {

#ifndef ONLINE_JUDGE
    close (0) ; open ("test.txt", O_RDONLY);
    close (1) ; open ("result.txt", O_WRONLY | O_CREAT, 0600);
#endif

    int field = 1;
    int rows, columns;
    while (cin >> rows >> columns){

        //check for end of input
        if (rows == 0 || columns == 0){
            //cout << "break";
            break;
        }
        if(field > 1)
            cout << endl << endl;

        //Dynamic field declaration and initialization
        int **mineField;
        mineField = new int*[rows];
        for (int i = 0; i < rows; i++){
            mineField[i] = new int[columns];
        }
        for(int i = 0; i < rows; i++)
            for(int j = 0; j < columns; j++)
                mineField[i][j] = 0;

        //main iteration loop
        for (int i = 0; i < rows; i++){
            string line;
            cin >> line;
            //cout << "echo: " << line << endl;

            for (int j = 0; j < columns; j++){
                if (line[j] == '*')
                    updateField(mineField, i,j,rows,columns);
            }
        }


        //print out results
        cout << "Field #" << field << ":" << endl;
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++)
                if(mineField[i][j] < 0)
                    cout << '*';
                else
                    cout << mineField[i][j];
            if (i != (rows -1))
                cout << endl;
        }
        field++;

        //Dymanic array memory release
        for (int i = 0; i < rows; i++){
            delete[] mineField[i];
        }
        delete[] mineField;
        mineField = 0;
    }
    return 0;
}
eyefinity
New poster
Posts: 1
Joined: Mon Apr 18, 2011 6:27 pm

Re: 10189 - Minesweeper

Post by eyefinity »

Can someone help me with this code.. am getting WA....

Code: Select all

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	//freopen("input.txt", "r+", stdin);
	//freopen("output.txt", "w+", stdout);

	long row, column, cases=1;
	
	while (true)
	{
		cin >> row >> column;
		if(row==0 && column ==0) break;

		char array[150][150];
		int ans[152][152]={0};

		for (int i=0; i < row; i++)
			for (int j=0; j < column; j++)
			{
				cin >> array[i][j];
				if(array[i][j]=='*') ans[i+1][j+1]=1;
			}
		for (int i=0; i < row; i++)
		{
			for (int j=0; j < column; j++)
			{
				if(array[i][j]=='.')
				{
					array[i][j] = ans[i][j+1]+ans[i+2][j+1]+ans[i+1][j]+ans[i+1][j+2]
					+ans[i][j]+ans[i+2][j+2]+ans[i+2][j]+ans[i][j+2] + 48;
				}
			}
		}
		cout << "Field #" << cases << ":" << endl ;
		for (int i=0; i < row; i++)
		{
			for (int j=0; j < column; j++)
			{
				cout << array[i][j];
			}
			cout << endl;
		}
		cases++;
		cout << endl;
	}

	return 0;
}
muctadir.dinar
New poster
Posts: 4
Joined: Sat Apr 16, 2011 10:04 pm
Location: IIT, DU
Contact:

Re: 10189 - Minesweeper

Post by muctadir.dinar »

getting wa with code below,

Code: Select all

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main()
{
    long int row, column;
    int fieldCount = 0;

    while (scanf("%ld %ld", &row, &column)!=EOF)
    {
        if (row == 0 && column == 0)
        {
            break;
        }

        char input[row+100][column+100];

        getchar();

        if (fieldCount!=0)
        {
            printf("\n");
        }

        fieldCount++;

        for (int r = 0; r<row; r++)
        {
            gets(input[r]);
        }
        printf("Field #%d:\n", fieldCount);


        char result[row+100][column+100];
        for (int r = 0; r<row; r++)
        {
            int c;
            for (c = 0; c<column; c++)
            {
                int mineCount = 0;

                if (input[r][c]!='*')
                {

                    if (input[r][c+1] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r][c-1] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r+1][c] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r+1][c+1] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r+1][c-1] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r-1][c] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r-1][c+1] == '*')
                    {
                        mineCount++;
                    }

                    if (input[r-1][c-1] == '*')
                    {
                        mineCount++;
                    }

                    result[r][c] = 48+mineCount;

                }
                else
                {
                    result[r][c] = '*';
                }
            }
            result[r][c] = NULL;
        }

        for (int i = 0; i<row; i++)
        {
            for (int j = 0; j<column; j++)
            {
                printf("%c", result[i][j]);
            }
            printf("\n");
        }
    }

    return 0;
}



You are the one, who can make a difference...
lazzrov
New poster
Posts: 1
Joined: Tue Feb 22, 2011 2:34 am

Re: 10189 - Minesweeper

Post by lazzrov »

I'm getting WA. I went through all the previous posts to check for possible errors, tried everything and I can't seem to get a AC. There was some post that a conversion to C++ would get the problem accepted.
I tried many test cases and the algorithm should be correct.

Code: Select all

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int dx[]=new int[]{0,0,1,-1,-1,-1,1,1};
		int dy[]=new int[]{1,-1,0,0,-1,1,-1,1};
		int num=1;
		int ln=0;
		int a=0;
		int b=0;
		while(true){
			String g;
			String nums[];
			if(ln>0)System.out.println();
			ln++;
			 	g=s.nextLine();
				nums=g.split(" ");
				a=Integer.parseInt(nums[0]);
				b=Integer.parseInt(nums[1]);
			
			
			if(a==0&&b==0)break;
			int map[][]=new int[a][b];
			
			for (int i = 0; i < a; i++) {
				String str=s.nextLine();
				for (int j = 0; j < b; j++) {
					if(str.charAt(j)=='*'){
						map[i][j]=-1;
					}
				}
			}
			
			for (int i = 0; i < a; i++) {
				for (int j = 0; j < b; j++) {
					int t=map[i][j];
					if(t!=-1)continue;
					
					for (int k = 0; k < 8; k++) {
						int ii=i+dx[k];
						int jj=j+dy[k];
						if(ii<0||ii>=a||jj<0||jj>=b)continue;
						if(map[ii][jj]!=-1){
							map[ii][jj]++;
						}
					}
					
				}
			}
			
			System.out.println("Field #"+num+":");
			num++;
			for (int i = 0; i < a; i++) {
				for (int j = 0; j < b; j++) {
					if(map[i][j]!=-1)System.out.print(map[i][j]);
					else System.out.print("*");
				}
				System.out.println();
			}
			
			
		}
		
	}

}

huynhlv_54
New poster
Posts: 1
Joined: Sun Jun 19, 2011 2:50 pm

Presentation error.

Post by huynhlv_54 »

I tried many times also. It turned out that after the last output, there must not a blank line
Caseh89
New poster
Posts: 1
Joined: Wed Jun 29, 2011 1:07 am

Re: 10189 - Minesweeper

Post by Caseh89 »

I've been fiddling with the output (see - a few commented out cout's) for a hours now, and no luck! It gives the right output given a set of parameters, (I've scoured google and the rest of this thread for ideas), but the online judge is unforgiving of whatever mistake I seem to be making. :(

Code: Select all

solved! 8ms, bling!
(And I know it isn't super optimized, and it's probably full of bad habits/poor conventions that I've managed to pick up already, but I'm new to C++ I'm just trying to get it to work!) (And I know that I'm eating up RAM by not deleting (after new), but deleting it threw up some funny exception errors, so I'm leaving that issue for now). And that using letters -> numbers is a bit silly, but whenever I tried to increment 0 as a char it just changed to a smiley face. :s Couldn't find that on google!

I'm refreshing the mineSweepArray with data in each loop, so I think that's ok. I've got the exit clause sorted too.

Anyone have any ideas? I might try making it using a 2D array, but I don't think that's the problem! I think there might be some issue with a funny shaped array, that might be messing up my 1D in 2D setup, but I've tried 1*x and x*1 and they still seem to work happily. :s

Should the last line be like this after 0 0 has been inputted if you look at it in console using break before the final return?:

***1_

^ With the cursor (underscore in example) still at the end of the last generated line - ie no std::endl?

If anyone still checks this thread, (I realize it's been around for years), any help would be awesome! :) It's driving me up the wall! I've half a mind to pickup sticks and move onto #3! But I hate to leave my second program unsolved. :/

Thanks, and sorry if I ranted a bit!

Casey

-- Update - I solved it, shouldn't have made my array static, must not have been wiping properly for some reason. :s Just needed a couple of days away to see it!
sabquat
New poster
Posts: 3
Joined: Fri Aug 19, 2011 11:19 am

10189-Minesweeper :WA

Post by sabquat »

plz,someone shoot my trouble :-?

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int l,c,p,b,q,i=0,f=1;
    char **n;
    while(scanf("%d %d",&l,&c)==2&&l!=0&&c!=0)
    {
        n=(char**)malloc(l*sizeof(char*));
        for(b=0;b<l;b++)
            n[b]=(char*)malloc((c+1)*sizeof(char));
        for(p=0;p<l;p++)
            scanf("%s",n[p]);
        printf("Field #%d:\n",f);
        for(p=0;p<l;p++)
        {
            for(q=0;q<c;q++)
            {
                if(n[p][q]!='*')
                {
                    if(p!=0&&q!=0&&n[p-1][q-1]=='*')
                        i++;
                    if(p!=0&&n[p-1][q]=='*')
                        i++;
                    if(p!=0&&q!=c-1&&n[p-1][q+1]=='*')
                        i++;
                    if(q!=0&&n[p][q-1]=='*')
                        i++;
                    if(q!=c-1&&n[p][q+1]=='*')
                        i++;
                    if(p!=l-1&&q&&n[p+1][q-1]=='*')
                        i++;
                    if(p!=l-1&&n[p+1][q]=='*')
                        i++;
                    if(p!=l-1&&q!=c-1&&n[p+1][q+1]=='*')
                        i++;
                    printf("%d",i);
                    i=0;
                }
                else
                    printf("*");
            }
            printf("\n");
        }
        f++;
        printf("\n");
    }
    return 0;
}
faiyaz26
New poster
Posts: 2
Joined: Sun Aug 28, 2011 12:44 am

Re: 10189 - Minesweeper

Post by faiyaz26 »

here is my code.. i dont know why i am getting WA !! whats da problem ??? help me guyzzz

Code: Select all

  /* Bismillahir Rahmanir Rahim */
                               /*Coder: Ahmad Faiyaz*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

# define FOR(i, a, b) for (int i=a; i<b; i++)
# define REP(i, a) FOR(i,0,a)

#define EPS 1e-11
#define inf ( 1LL << 31 ) - 1
#define LL long long

#define abs(x) (((x)< 0) ? (-(x)) : (x))
#define all(x) (x).begin(), (x).end()
#define ms(x, a) memset((x), (a), sizeof(x))

# define VI vector<int>
# define VS vector<string>
# define VC vector<char>

#define mp make_pair
#define pb push_back
#define sz(k) (int)(k).size()

using namespace std;
char table [120][120];

int main(){
    freopen("in.txt","r",stdin);
    int m,n;
    int t=1;
    while(cin>>m>>n){
        if(m==0 && n==0)
        break;
        else{
            if(t!=1)
            cout<<endl;

        ms(table,'0');

        for(int i=3;i<m+3;i++)
        for(int j=3;j<n+3;j++)
        cin>>table[i][j];

        for(int i=3;i<m+3;i++){
            for(int j=3;j<n+3;j++){
                if(table[i][j]!='*'){
                    int count=0;
                    for(int p=i-1;p<=i+1;p++){
                        for(int q=j-1;q<=j+1;q++){
                            if(table[p][q]=='*')
                            count++;
                        }
                    }
                    table[i][j]=count+'0';
                }
            }
        }

        printf("Field #%d:\n",t++);
        for(int i=3;i<m+3;i++){

            for(int j=3;j<n+3;j++){
                cout<<table[i][j];
            }
            //if(i+1<m+3)
            cout<<endl;
        }
     //cout<<endl;

        }
    }
}
Post Reply

Return to “Volume 101 (10100-10199)”