Page 15 of 28

Re: 10189 - Minesweeper

Posted: Wed Oct 06, 2010 8:00 pm
by fkrafi
WHy WA????

Code: Select all

Solved...

Re: 10189 - Minesweeper

Posted: Wed Oct 27, 2010 4:23 am
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;
}

10189 Minesweeper RE

Posted: Sun Oct 31, 2010 4:16 pm
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

Minesweeper --problem 10189 WA

Posted: Tue Nov 02, 2010 8:38 pm
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....

Re: 10189 - Minesweeper

Posted: Tue Nov 30, 2010 12:03 am
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.

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

Posted: Sat Jan 08, 2011 6:52 pm
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?

Re: 10189 - Minesweeper

Posted: Mon Mar 14, 2011 5:01 am
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;

Re: 10189 - Minesweeper

Posted: Sun Mar 20, 2011 6:30 pm
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;
}

Re: 10189 - Minesweeper

Posted: Mon Apr 18, 2011 6:31 pm
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;
}

Re: 10189 - Minesweeper

Posted: Tue Apr 19, 2011 10:30 am
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;
}




Re: 10189 - Minesweeper

Posted: Mon Apr 25, 2011 6:19 pm
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();
			}
			
			
		}
		
	}

}


Presentation error.

Posted: Sun Jun 19, 2011 2:53 pm
by huynhlv_54
I tried many times also. It turned out that after the last output, there must not a blank line

Re: 10189 - Minesweeper

Posted: Wed Jun 29, 2011 2:08 am
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!

10189-Minesweeper :WA

Posted: Fri Aug 19, 2011 11:41 am
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;
}

Re: 10189 - Minesweeper

Posted: Sun Aug 28, 2011 12:46 am
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;

        }
    }
}