10189 - Minesweeper
Moderator: Board moderators
-
- New poster
- Posts: 42
- Joined: Fri Jun 13, 2003 3:47 pm
- Location: Dhaka , Bangladesh
- Contact:
-
- New poster
- Posts: 1
- Joined: Wed Mar 07, 2007 9:35 pm
10189 - Output Limit Exceeded!!
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?
/***********************
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?

-
- New poster
- Posts: 8
- Joined: Sat Mar 10, 2007 10:20 am
10189 WA?? PLZ HELP!!
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!!!:)
"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;
}
10189 -- Minesweeper
Folks,
This is my code in Java. After struggling for 1 day to solve "Compile Error" now I get "Wrong Answer"
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)
{
;
}
}
}
This is my code in Java. After struggling for 1 day to solve "Compile Error" now I get "Wrong Answer"

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)
{
;
}
}
}
Reading a variable length input
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
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
:-?
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 -> ....
This should work!
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
}
WA-Help
I took in the input in a Long array,and checked 8 particular case.
Couldn't understand,why wrong answer.
Plz help me.
Couldn't understand,why wrong answer.
Plz help me.
10189 in Java - WA
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
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
Test the cases. If your code passes then post your code.
Input:
Output:
Hope these help.
Input:
Code: Select all
5 5
*****
*...*
*.*.*
*...*
*****
4 4
****
*..*
*..*
****
0 0
Code: Select all
Field #1:
*****
*646*
*4*4*
*646*
*****
Field #2:
****
*55*
*55*
****
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 38
- Joined: Tue Jul 17, 2007 3:21 pm
- Location: East West University
simple problem,simple code, but getting WA. where is the bug? plz tell me.
thanks
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
Test the cases.
Input:
Output:
Hope these help.
Input:
Code: Select all
5 5
*****
*...*
*.*.*
*...*
*****
4 4
*.*.
.***
**..
.*.*
0 0
Code: Select all
Field #1:
*****
*646*
*4*4*
*646*
*****
Field #2:
*4*3
4***
**63
3*3*
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 38
- Joined: Tue Jul 17, 2007 3:21 pm
- Location: East West University
hi jan,
i change my code and it shows the right answer of the inputs that you have given. but still wrong answer.
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