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

arif_pasha
New poster
Posts: 42
Joined: Fri Jun 13, 2003 3:47 pm
Location: Dhaka , Bangladesh
Contact:

Post by arif_pasha » Mon Mar 05, 2007 11:26 am

1. use gets() to get the line.
2. use larger array size [say 110]

Hope it helps..

User avatar
KaDeG
New poster
Posts: 13
Joined: Sun Mar 04, 2007 8:40 pm

Post by KaDeG » Mon Mar 05, 2007 1:32 pm

ok, the problem was only the Max of the tables i make the tables at 110 and it was accepted, thanks, real thanks arif pasha, it's also works with Tables with 101!
/*No Comment*/

Amichelg75
New poster
Posts: 1
Joined: Wed Mar 07, 2007 9:35 pm

10189 - Output Limit Exceeded!!

Post by Amichelg75 » Wed Mar 07, 2007 9:42 pm

Hello, My code is:

/***********************
solution to problem ,
by: Adrian Michel
date: 06/03/2007
***********************/

#include<stdio.h>
#include<string.h>
main(){

char Minesweeper[100][100];
int NFilas, NColumnas, li, lj, ContBombs, ContFields;

ContFields = 1;

do{
/* Captura del Numero de Filas y Columnas */
scanf("%d %d", &NFilas, &NColumnas);
fflush(stdin);
/* Captura del tablero de Minesweeper */
if( (NFilas > 0) && (NColumnas > 0) )
{

for ( li = 0; li < NFilas; li ++ )
{

for ( lj = 0; lj < NColumnas; lj++ )
{
scanf("%c", &Minesweeper[li][lj]);
}
fflush(stdin);
} /*Fin Captura*/

/* Transformacion de la Matriz*/
for ( li = 0;li < NFilas; li++ )
{

for ( lj = 0; lj < NColumnas; lj++)
{

if ( Minesweeper[li][lj] =='.' )
{

ContBombs = 0;

if ( (li-1 >=0) && (lj-1 >= 0) &&
( Minesweeper[li-1][lj-1] == '*' ) )
{
ContBombs ++;
}
if ( (lj-1 >= 0) &&
( Minesweeper[li][lj-1] == '*' ) )
{
ContBombs ++;
}
if ( (li+1 < NFilas) && (lj-1 >= 0) &&
( Minesweeper[li+1][lj-1] == '*' ) )
{
ContBombs ++;
}
if ( (li-1 >=0) && (lj+1 < NColumnas) &&
( Minesweeper[li-1][lj+1] == '*' ) )
{
ContBombs ++;
}
if ( (lj+1 < NColumnas) &&
( Minesweeper[li][lj+1] == '*' ) )
{
ContBombs ++;
}
if ( (li+1 < NFilas) && (lj+1 < NColumnas) &&
( Minesweeper[li+1][lj+1] == '*' ) )
{
ContBombs ++;
}
if ( (li-1 >= 0) &&
( Minesweeper[li-1][lj] == '*' ) )
{
ContBombs ++;
}
if ( (li+1 < NFilas) &&
( Minesweeper[li+1][lj] == '*' ) )
{
ContBombs ++;
}
switch (ContBombs)
{
case 0:
Minesweeper[li][lj] = '0';
break;
case 1:
Minesweeper[li][lj] = '1';
break;
case 2:
Minesweeper[li][lj] = '2';
break;
case 3:
Minesweeper[li][lj] = '3';
break;
case 4:
Minesweeper[li][lj] = '4';
break;
case 5:
Minesweeper[li][lj] = '5';
break;
case 6:
Minesweeper[li][lj] = '6';
break;
case 7:
Minesweeper[li][lj] = '7';
break;
case 8:
Minesweeper[li][lj] = '8';
break;
} /*Fin Switch*/
} /*Fin Si '.'*/

} /*Fin For lj*/
}/*Fin For li*/

/*Mostrar Resultados*/
printf ("Field #%d:\n", ContFields);
for ( li = 0;li < NFilas; li++ )
{

for ( lj = 0; lj < NColumnas; lj++)
{
printf ("%c", Minesweeper[li][lj]);
}
printf("\n");
} /*Fin For Imprime*/

printf ("\n");
ContFields ++;
}
}while(NFilas > 0 && NColumnas > 0);
return 0;
}

online-Judged Output Limit Exceeded!!, Why? :-?

deadhunter411
New poster
Posts: 8
Joined: Sat Mar 10, 2007 10:20 am

10189 WA?? PLZ HELP!!

Post by deadhunter411 » Tue Mar 20, 2007 10:38 am

about the ouput "\n"
"There must be an empty line between field outputs."
Am I right when input is "0 0" ??

thank you for help!!!:)

Code: Select all

#include<stdio.h>

int main()
{
	int k,l,size_1,size_2,i,j,I,J,boom=0,num=1,end=0;
	char a[200][200];
	char b[200][200];

	scanf(" %d %d",&size_1,&size_2);

	while(size_1!=0 && size_2!=0)
	{

		for(k=0;k<size_1;k++)	
			for(l=0;l<size_2;l++)
				scanf(" %c",&a[k][l]);	

		for(i=0;i<size_1;i++)
		{
			for(j=0;j<size_2;j++)
			{				
				if(a[i][j]=='*')
				{
					b[i][j]=a[i][j];
					continue;
				}
				else if(a[i][j]=='.')
				{
					for(I=i-1;I<i+2;I++)
					{
						for(J=j-1;J<j+2;J++)
						{
							if(I<0 || J<0 || I==i && J==j)
								continue;
							else if(a[I][J]=='*')
								boom++;
						}
					}
					b[i][j]=boom;
					boom=0;
				}			
			}
		}

		if(end)
			printf("\n");

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

		for(k=0;k<size_1;k++)
		{	
			for(l=0;l<size_2;l++)
			{		
				if(b[k][l]=='*')
					printf("%c",b[k][l]);
				else
					printf("%d",b[k][l]);
			}
			printf("\n");
			
		}

		end=1;

		scanf(" %d %d",&size_1,&size_2);

	}
	return 0;
}

roozbeh84
New poster
Posts: 1
Joined: Thu Apr 12, 2007 7:51 pm

10189 -- Minesweeper

Post by roozbeh84 » Fri Apr 13, 2007 7:14 pm

Folks,

This is my code in Java. After struggling for 1 day to solve "Compile Error" now I get "Wrong Answer" :o

Anybody PLEASE can tell me where am I wrong?
Thx...





/*
* Main.java
* java program model for http://www.programming-challenges.com
*/

import java.io.*;
import java.util.*;

class Main implements Runnable{
static String ReadLn(int maxLength){ // utility function to read from stdin,
// Provided by Programming-challenges, edit for style only
byte line[] = new byte [maxLength];
int length = 0;
int input = -1;
try{
while (length < maxLength){//Read untill maxlength
input = System.in.read();
if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
line [length++] += input;
}

if ((input < 0) && (length == 0)) return null; // eof
return new String(line, 0, length);
}catch (IOException e){
return null;
}
}

public static void main(String args[]) // entry point from OS
{
Main myWork = new Main(); // Construct the bootloader
myWork.run(); // execute
}

public void run() {
new myStuff().run();
}
}

class myStuff implements Runnable{
public void run(){
// Your program here

try
{

//BufferedReader br = new BufferedReader(new FileReader("minesweeper.txt"));



String line = "";
StringTokenizer st;
int n = 0;
int m = 0;
int counter = 0;

while(true)
{
line = Main.ReadLn(50);
st = new StringTokenizer(line);

n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());

//System.out.println(n + " " + m);
if(n==0 && m==0)
break;

String[][] arr = new String[110][110];
counter++;

for(int i=0;i<110;i++)
{
for(int j=0;j<m;j++)
{
arr[j] = String.valueOf(0);
}
}

int mcount = 0;
for(int i=0;i<n;i++)
{

mcount = 0;
line = Main.ReadLn(50);

char[] chars = line.toCharArray();
int a=0;

while(a<m)
{
if(chars[a]=='*')
{
arr[mcount] = "*";
if(i+1<=n)
{
try
{
int val = Integer.parseInt(arr[i+1][mcount]);
val++;
arr[i+1][mcount] = String.valueOf(val);
}
catch(NumberFormatException n1)
{
;
}
}
if(i-1>=0)
{
try
{
int val = Integer.parseInt(arr[i-1][mcount]);
val++;
arr[i-1][mcount] = String.valueOf(val);
}
catch(NumberFormatException n2)
{

}
}
if(mcount+1<=m)
{

try
{
int val = Integer.parseInt(arr[mcount+1]);
val++;
arr[mcount+1] = String.valueOf(val);
}
catch(NumberFormatException n3)
{

}
}
if(mcount-1>=0)
{

try
{
int val = Integer.parseInt(arr[mcount-1]);
val++;
arr[mcount-1] = String.valueOf(val);
}
catch(NumberFormatException n3)
{

}
}
if(i+1<=n && mcount+1<=m)
{
try
{
int val = Integer.parseInt(arr[i+1][mcount+1]);
val++;
arr[i+1][mcount+1] = String.valueOf(val);
}
catch(NumberFormatException n4)
{

}
}
if(i+1<=n && mcount-1>=0)
{
try
{
int val = Integer.parseInt(arr[i+1][mcount-1]);
val++;
arr[i+1][mcount-1] = String.valueOf(val);
}
catch(NumberFormatException n5)
{
;
}
}
if(i-1>=0 && mcount+1<=m)
{
try
{
int val = Integer.parseInt(arr[i-1][mcount+1]);
val++;
arr[i-1][mcount+1] = String.valueOf(val);
}
catch(NumberFormatException n6)
{
;
}
}
if(i-1>=0 && mcount-1>=0)
{
try
{
int val = Integer.parseInt(arr[i-1][mcount-1]);
val++;
arr[i-1][mcount-1] = String.valueOf(val);
}
catch(NumberFormatException n7)
{
;
}
}
}
mcount++;
a++;
}

}




System.out.println("Field #" + counter + ":");
for(int l=0;l<n;l++)
{
for(int j=0;j<m;j++)
{
System.out.print(arr[l][j]);
}
System.out.println();
}

System.out.println();


}


// You can insert more classes here if you want.


}
catch(Exception e)
{
;
}

}
}

Ahadoom
New poster
Posts: 4
Joined: Sun Apr 15, 2007 1:28 am

Reading a variable length input

Post by Ahadoom » Thu Apr 26, 2007 4:18 pm

Hi everyone.

I was looking at this problem and i cant figure a way to store all input without allocating memory dynamically. If somebody could help i would appreciate.

TIA

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

:-?

Post by sohel » Thu Apr 26, 2007 6:10 pm

Why do you have to store all the inputs?

Just take one case -> process it -> print the result -> take next case, reusing the old array -> ....

Code: Select all

   char input[101][101];
   int r, c, i;
   while( scanf("%d %d", &r, &c) == 2 && (r||c) ) {
       for(i=0;i<r;i++) scanf("%s", input[i]);
       // process
       // print output
   }

This should work!

Ahadoom
New poster
Posts: 4
Joined: Sun Apr 15, 2007 1:28 am

Post by Ahadoom » Sun Apr 29, 2007 2:09 am

My mistake, i just realised there is a maximum line limit.

But what if no limit is given.

PS: Of course i was not planning to store all the input, just the current input.

Neli
New poster
Posts: 7
Joined: Thu May 03, 2007 8:35 am
Location: Sylhet
Contact:

WA-Help

Post by Neli » Sun May 13, 2007 8:24 am

I took in the input in a Long array,and checked 8 particular case.
Couldn't understand,why wrong answer.
Plz help me.

User avatar
dttri
New poster
Posts: 3
Joined: Thu Apr 05, 2007 7:13 pm
Location: Vietnam

10189 in Java - WA

Post by dttri » Sun May 13, 2007 9:39 am

Hi everybody,
I search the forums but can't find any topic similar to mine. I'm sure about the "empty line between case" as many other topics. Is there any wrong with my code?

Thank you

Code: Select all


import java.io.*;
import java.util.*;

class Main
{
	//This is a utility function to read from stdin
	//provided by UVA: do not modify
	static String readLine (int maxLg)
	{
		byte lin[] = new byte [maxLg];
		int lg = 0, car = -1;
		String line = "";

		try
		{
			while (lg < maxLg)
			{
				car = System.in.read();
				if ((car < 0) || (car == '\n')) break;
				lin [lg++] += car;
			}
		}
		catch (IOException e)
		{
			return (null);
		}

		if ((car < 0) && (lg == 0)) return (null);  // eof
		return (new String (lin, 0, lg));
	}
	
	static void writeLine(String s)
	{
		System.out.println(s);
	}
	
	static void writeLine()
	{
		System.out.println();
	}
	
	
	//entry point from OS
	public static void main (String[] args)
	{
		Main myWork = new Main();  // create a dinamic instance
		myWork.begin();            // the true entry point
	}

	void begin()
	{
		String input;
		StringTokenizer idata;
		char[][] cell;
		char[][] output;
		int row,col;
		int caseNumber=1;
		while(true)
		{
			input = readLine(255);
			idata = new StringTokenizer(input);
			row = Integer.parseInt(idata.nextToken());
			col = Integer.parseInt(idata.nextToken());
			if(row==0 && col==0)break;
			cell = new char[row][col];
			output = new char[row][col];
			//read all row and assign output
			for(int i=0;i<row;i++)
			{
				input=readLine(255);
				//assign value for each cell
				for(int j=0;j<col;j++)
				{
					cell[i][j]=input.charAt(j);
					output[i][j]='0';
				}
			}
			//process all cell
			for(int i=0;i<row;i++)
			{
				for(int j=0;j<col;j++)
				{
					if(cell[i][j]=='*')
					{
						output[i][j]='*';
						continue;
					}
					for(int x=-1;x<=1;x++)
					{
						for(int y=-1;y<=1;y++)
						{
							if(x==0 && y==0)continue;
							if(i+x<0 || i+x>=row || j+y<0 || j+y>=col)continue;
							if(cell[i+x][j+y]=='*')output[i][j]++;
						}
					}
				}
			}
			if(caseNumber!=1)
			{
				writeLine();
			}
			writeLine("Field #"+caseNumber+":");
			for(int i=0;i<row;i++)
			{
				String s=new String(output[i]);
				writeLine(s);
			}
			caseNumber++;
		}
	}
}

//Powered by myself

User avatar
dttri
New poster
Posts: 3
Joined: Thu Apr 05, 2007 7:13 pm
Location: Vietnam

Post by dttri » Sun May 13, 2007 6:33 pm

I copy my Java code above and translate it into C++ and I got Accepted. Don't know why the Java version doesn't work. I think the Java judge here is not well supported. And I don't know why UVA use GCJ instead of Java from Sun?

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sun May 13, 2007 10:19 pm

Test the cases. If your code passes then post your code.

Input:

Code: Select all

5 5
*****
*...*
*.*.*
*...*
*****
4 4
****
*..*
*..*
****
0 0
Output:

Code: Select all

Field #1:
*****
*646*
*4*4*
*646*
*****

Field #2:
****
*55*
*55*
****
Hope these help.
Ami ekhono shopno dekhi...
HomePage

User avatar
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

Post by Fuad Hassan EWU » Tue Jul 17, 2007 9:14 pm

simple problem,simple code, but getting WA. where is the bug? plz tell me.
thanks

Code: Select all

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

using namespace std;

int main()
{
	int m,n,i,j,count,flag=0,field=1;
	char a[109][109];

	while(cin>>m>>n)
	{
		 if(m==0&&n==0)
			 break;
		 if(flag==1)
			 cout<<endl;

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

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


Eagle er moto daana meley urbo

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Tue Jul 17, 2007 9:33 pm

Test the cases.

Input:

Code: Select all

5 5
*****
*...*
*.*.*
*...*
*****
4 4
*.*.
.***
**..
.*.*
0 0
Output:

Code: Select all

Field #1:
*****
*646*
*4*4*
*646*
*****

Field #2:
*4*3
4***
**63
3*3*
Hope these help.
Ami ekhono shopno dekhi...
HomePage

User avatar
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

Post by Fuad Hassan EWU » Tue Jul 17, 2007 10:16 pm

hi jan,

i change my code and it shows the right answer of the inputs that you have given. but still wrong answer.
code removed after AC
Last edited by Fuad Hassan EWU on Wed Jul 18, 2007 5:55 pm, edited 1 time in total.
Eagle er moto daana meley urbo

Post Reply

Return to “Volume 101 (10100-10199)”