10189 - Minesweeper

All about problems in Volume CI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 10189 - Minesweeper

Postby lazzrov » Mon Apr 25, 2011 6:19 pm

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();
         }
         
         
      }
      
   }

}

lazzrov
New poster
 
Posts: 1
Joined: Tue Feb 22, 2011 2:34 am

Presentation error.

Postby huynhlv_54 » Sun Jun 19, 2011 2:53 pm

I tried many times also. It turned out that after the last output, there must not a blank line
huynhlv_54
New poster
 
Posts: 1
Joined: Sun Jun 19, 2011 2:50 pm

Re: 10189 - Minesweeper

Postby Caseh89 » Wed Jun 29, 2011 2:08 am

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!
Caseh89
New poster
 
Posts: 1
Joined: Wed Jun 29, 2011 1:07 am

Re: 10189 - Minesweeper

Postby faiyaz26 » Sun Aug 28, 2011 12:46 am

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;

        }
    }
}
faiyaz26
New poster
 
Posts: 1
Joined: Sun Aug 28, 2011 12:44 am

Re: 10189 - Minesweeper

Postby Zopper » Mon Oct 10, 2011 1:17 am

Alright, I'm getting RE (runtime) errors, anyone see anything wrong here?

Code: Select all
#include <iostream>
#include <string.h>

int main()
{
   int x=-1,y=-1;
   int count=1;

   while(std::cin >> x >> y)
   {
      if(x==0&&y==0)
         return 0;

      char field[10000][10000];
      //*field=new char[x];

      //for(int i=0; i<x; i++)
         //field[i]=new char[y];

      memset(field, '\0', sizeof(field));

      for(int i=1; i<=x; i++)
         for(int j=1; j<=y; j++)
            std::cin >> field[i][j];

      std::cout << "Field #" << count << ":\n";

      for(int i=1; i<=x; i++)
      {
         for(int j=1; j<=y; j++)
         {
            if(field[i][j]=='*')
            {
               std::cout << "*";
            }

            else
            {
               int tmp=0;

               if(field[i-1][j]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i+1][j]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i][j-1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i][j+1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i+1][j+1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i-1][j-1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i+1][j-1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               if(field[i-1][j+1]=='*'&&field[i-1][j]!='\0')
                  tmp++;

               std::cout << tmp;
            }
         }

         std::cout << "\n";
      }

      std::cout << "\n";

      count++;

      delete field;
   }
}
Zopper
New poster
 
Posts: 1
Joined: Mon Oct 10, 2011 1:11 am

Re: 10189 - Minesweeper

Postby datahaven » Thu Nov 10, 2011 3:26 am

This!

Adrian

Zopper wrote:Alright, I'm getting RE (runtime) errors, anyone see anything wrong here?

Code: Select all
      delete field;
datahaven
New poster
 
Posts: 4
Joined: Thu Nov 10, 2011 3:17 am

WA since i was born

Postby brwnow » Sat Nov 19, 2011 4:13 am

Well, i'm not a good english speaker, so i won't say a lot, just the enough

getting WA, and i can't understand the why

input:
Code: Select all
3 3
..*
*..
.*.
8 8
.....*.*
*....*..
........
..***...
..***...
..***.*.
......*.
*....*..
0 0


output:
Code: Select all
Field #1:
12*
*32
2*1

Field #2:
11002*3*
*1002*31
12233210
02***200
03***411
02***4*2
122334*2
*1001*21
brwnow
New poster
 
Posts: 1
Joined: Sat Nov 19, 2011 4:05 am

why WA?

Postby armankashef » Tue Jun 05, 2012 9:41 pm

my code is:


#include <iostream>
#include <string.h>
using namespace std;
char ch[101][101];
int mat[101][101] , dir[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}} , n , m ;
void f()
{
memset( mat , 0 , sizeof( mat ) );
int i , j , k , r , s;
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
for( k = 0 ; k < 8 ; k ++ )
{
r = i + dir[k][0];
s = j + dir[k][1];
if( ( r >= 0 && s >= 0 && r < n && s < m ) && ch[i][j] == '*' )
mat[r][s] ++;
}
}
int main()
{
int i , j , c = 1;
while( cin >> n >> m , n || m )
{
for( i = 0 ; i < n ; i ++ )
for( j = 0 ; j < m ; j ++ )
cin >> ch[i][j];
f();
cout << "Field #" << c ++ << ':' << endl;
for( i = 0 ; i < n ; i ++ )
{
for( j = 0 ; j < m ; j ++ )
{
if( ch[i][j] == '*' )
cout << '*';
else
cout << mat[i][j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
armankashef
New poster
 
Posts: 10
Joined: Tue Jun 05, 2012 9:33 pm

Re: 10189 - Minesweeper

Postby brianfry713 » Wed Jun 06, 2012 10:51 pm

Don't print a blank line at the end of the output.
brianfry713
Guru
 
Posts: 1755
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper WA???

Postby luimiruiz » Tue Jul 03, 2012 7:08 am

Code: Select all
#include <iostream>
using namespace std;
int main()
{
   int dx[] = { 1, 1, 1, 0, 0, -1, -1, -1 };
   int dy[] = { 1, -1, 0, 1, -1, 1, 0, -1 };
   int n, m, x = 1;
   char aux;
   while(1)
   {
      cin >>n >> m;
      if(n == 0 && m == 0)return 0;
      char arr[102][102];
      for(int i = 1; i <= n; ++i)
      {
         for(int j = 1; j <= m; ++j)
         {
            cin >> aux;
            if(aux == '.')
               arr[i][j]='0';
            else
               arr[i][j]= aux;
         }
      }
      for(int i = 1; i <= n; ++i)
      {
         for(int j = 1; j <= m; ++j)
         {
            if(arr[i][j] != '*')continue;
            for(int x = 0; x < 8; ++x)
            {
               if(arr[i+dx[x]][j+dy[x]] != '*')
                  arr[i+dx[x]][j+dy[x]]++;
            }
         }
      }
      cout << "Field #" << x <<':'<<endl;
      ++x;
      for(int i = 1; i <= n; ++i)
      {

         for(int j = 1; j <= m; ++j)
         {
            cout << arr[i][j];

         }
         cout << endl;
      }
      cout << endl;
   }
}



Why WA????????? I dont understand
luimiruiz
New poster
 
Posts: 1
Joined: Tue Jul 03, 2012 7:05 am

Re: 10189 - Minesweeper

Postby brianfry713 » Thu Jul 05, 2012 7:12 am

Don't print an extra newline at the end of the output.
brianfry713
Guru
 
Posts: 1755
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Postby sumit saha shawon » Wed Aug 22, 2012 4:20 pm

My all input and output is ok but why I am getting WA.
My code:

#include<stdio.h>
#include<string.h>
char field[105][105];
int main()
{
int r,c,a=1,kase=1;
while(scanf("%d %d",&r,&c)==2)
{
if(r==0&&c==0)
break;
// memset(field,0,)

int i,j,count=0;
for(i=0; i<r; i++)
scanf("%s",&field[i]);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(field[i][j]=='*')
continue;
else
{
if(field[i][j-1]=='*')
count++;
if(field[i][j+1]=='*')
count++;
if(field[i+1][j]=='*')
count++;
if(field[i-1][j]=='*')
count++;
if(field[i+1][j-1]=='*')
count++;
if(field[i+1][j+1]=='*')
count++;
if(field[i-1][j+1]=='*')
count++;
if(field[i-1][j-1]=='*')
count++;
}
field[i][j]=count+'0';
count=0;
}
}
if(a!=1)
puts("");
//kase++;
printf("Field #%d:\n",kase++);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
printf("%c",field[i][j]);
puts("");

}

a=2;
}
return 0;
}
sumit saha shawon
New poster
 
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 10189 - Minesweeper

Postby brianfry713 » Thu Aug 23, 2012 2:40 am

Check your array boundaries. If i=j=0, don't try to read field[i-1][j-1].
brianfry713
Guru
 
Posts: 1755
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Postby badc0re » Thu Dec 27, 2012 2:42 pm

Code: Select all
solved the problem


Got WA, any hints or ideas?
Last edited by badc0re on Sun Dec 30, 2012 6:17 pm, edited 1 time in total.
badc0re
New poster
 
Posts: 2
Joined: Thu Dec 27, 2012 2:40 pm

Re: 10189 - Minesweeper

Postby brianfry713 » Sat Dec 29, 2012 9:57 pm

line 13: error: ‘memset’ was not declared in this scope, include <string.h>
line 15: warning: left-hand operand of comma has no effect
line 33: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’

There must be an empty line between field outputs.
Don't print a blank line at the end.
brianfry713
Guru
 
Posts: 1755
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

PreviousNext

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 0 guests