Page 12 of 28
Re: 10189 - Minesweeper
Posted: Thu Jul 10, 2008 9:05 pm
by Jan
You should print a blank line between cases, not after each case.
Code: Select all
...
int main()
{
...
int caseno=0;
for(;;)
{
cin>>row>>col;
if(row==0 && col==0)
{
break;
}
if(caseno++) cout<<endl; // I have added this line
...
//cout<<endl; // Removed this line
}
return 0;
}
Hope it helps.
Re: 10189 - Minesweeper
Posted: Sat Jul 12, 2008 7:11 pm
by jesun
THANKS a lot JAN as I got AC.You are truely a great helper.Thanks again for lending some of your valuable time to troubleshoot the problem.
Re: 10189 - Minesweeper
Posted: Sat Aug 02, 2008 6:39 pm
by lee.anna.loo
i have this common problem, it shows me "presentation error" on the
http://www.programming-challenges.com/. Now, I'm waiting for the results from UVa
i tried and to put an empty line after inputting the line and row, but it doesn't work like that either..
any suggestions what might help?
Code: Select all
#include<iostream>
using namespace std;
int main()
{
int field=1, m, n, i, j, br,a,b;
cin >> n >> m;
while(n!=0 && m!=0)
{
m+=2;
n+=2;
char mat[n][m];
for(i=0; i<n; i++)
for(j=0; j<m; j++)
mat[i][j]='.';
cout << endl;
for(i=1; i<n-1; i++)
for(j=1; j<m-1; j++)
cin >> mat[i][j];
cout << "Field #" << field << ":\n";
for(i=1; i<n-1; i++)
{
for(j=1; j<m-1; j++)
{
if(mat[i][j]=='*') cout << mat[i][j];
else
{
br=0;
for(a=i-1; a<=i+1; a++)
for(b=j-1; b<=j+1; b++)
if(mat[a][b]=='*')
br++;
cout << br;
}
}
cout << endl;
}
cin >> n >> m;
field++;
};
return 0;
}
Re: 10189 - Minesweeper
Posted: Sat Aug 16, 2008 6:40 pm
by Juli
Hi all, i got WA, but i don't understand why. Please help my.
Code: Select all
#include <iostream>
using namespace std;
int main()
{
int m = 1, n = 1, a[110][110], nom = 0;
char s[110];
while (m || n)
{
nom++;
cin >> m >> n;
for (int i = 1; i <= m; i++)
{
cin >> s;
for (int j = 0; j <= n; j++)
{
if (s[j] == '*')
a[i][j + 1] = -1;
if (s[j] == '.')
a[i][j + 1] = 0;
}
}
for (int i=0; i <= m; i++)
for (int j=0; j <= n; j++)
if (a[i][j] == -1)
{
if (a[i - 1][j - 1] != -1)
a[i - 1][j - 1] += 1;
if (a[i - 1][j] != -1)
a[i - 1][j] += 1;
if (a[i - 1][j + 1] != -1)
a[i - 1][j + 1] += 1;
if (a[i][j - 1] != -1)
a[i][j - 1] += 1;
if (a[i][j + 1] != -1)
a[i][j + 1] += 1;
if (a[i + 1][j - 1] != -1)
a[i + 1][j - 1] += 1;
if (a[i + 1][j] != -1)
a[i + 1][j] += 1;
if (a[i + 1][j + 1] != -1)
a[i + 1][j + 1] += 1;
}
if (n != 0 && m != 0)
cout << "Field #" << nom << ":" << endl;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j] == -1)
cout << "*"; else
cout << a[i][j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
Re: 10189 - Minesweeper
Posted: Thu Aug 21, 2008 10:28 am
by Obaida
Check this input output, you can see your mistake.
Code: Select all
5 5
*****
*...*
*.*.*
*...*
*****
Field #1:
*****
*646*
*4*4*
*646*
***** <<---------after this no blank line(but your code is printing a blank line).
4 4
<<-------a blank line.
*.*.
.***
**..
.*.*
Field #2:
*4*3
4***
**63
3*3*
Re: 10189 - Minesweeper
Posted: Mon Sep 01, 2008 4:37 pm
by syntax_error
WA...everything seems to be right...
Help!!
Code: Select all
#include <cstdio>
#include <cstring>
#define MAX 128
const int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const int dy[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int m = 1, n = 1;
char map[ MAX ][ MAX ];
char v[ MAX ][ MAX ];
char chr( int x ) { return x + '0'; }
void solve() {
for( int i = 0; i < n; ++i )
for( int j = 0; j < m; ++j ) {
if( map[i][j] == '*' ) continue; int cnt = 0;
for( int d = 0; d < 8; ++d ) {
int nx = i + dx[d], ny = j + dy[d];
if( nx < 0 || ny < 0 || nx >= n || ny >= m ) continue;
if( map[nx][ny] == '*' ) ++cnt;
}
map[i][j] = chr( cnt );
}
}
int main( void )
{
int field = 0;
while( 1 ) {
scanf( "%d%d", &n, &m );
if( !( n && m ) ) return 0;
for( int i = 0; i < n; ++i )
scanf( "%s", map[i] );
solve();
printf( "Field #%d:\n", ++field );
for( int i = 0; i < n; ++i ) {
for( int j = 0; j < m; ++j )
printf( "%c", map[i][j] );
printf( "\n" );
} printf( "\n" );
}
return 0;
}
Re: 10189 - Minesweeper
Posted: Sun Oct 12, 2008 9:03 pm
by Paiev
I get TLE, anyone know why?
Code: Select all
#include <iostream>
#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)
int main()
{
char grid[101][101];
int length, width;
int field = 1;
std::cin >> length >> width;
std::cin.get();
while (length != 0 && width != 0)
{
std::cin.get();
if (field > 1)
std::cout << std::endl;
for (int i = 0; i < length; ++i)
std::cin.getline(grid[i], 100);
std::cout << "Field #" << field << ":" << std::endl;
for (int i = 0; i < length; ++i)
{
for (int j = 0; j < width; ++j)
{
if (grid[i][j] == '*')
std::cout << '*';
else
{
int hint = 0;
for (int a = MAX(0, i - 1); a <= MIN(length - 1, i + 1); ++a)
for (int b = MAX(0, j - 1); b <= MIN(width - 1, j + 1); ++b)
if (grid[a][b] == '*')
++hint;
std::cout << hint;
}
}
std::cout << std::endl;
}
std::cin >> length >> width;
++field;
}
return 0;
}
Re: 10189 - Minesweeper
Posted: Mon Oct 13, 2008 11:52 am
by vinocit
Does any one know whats wrong in this?
#include<iostream>
using namespace std;
char check(char a[150][150],int i,int j,int m,int n)
{
if(a[j]=='*')return '*';
int x=0;
if(i>0)
{
if(j>0)
if(a[i-1][j-1]=='*')x++;
if(j<m-1)
if(a[i-1][j+1]=='*')x++;
if(a[i-1][j]=='*')x++;
}
if(i<n-1)
{
if(j>0)
if(a[i+1][j-1]=='*')x++;
if(j<m-1)
if(a[i+1][j+1]=='*')x++;
if(a[i+1][j]=='*')x++;
}
if(j>0)
if(a[j-1]=='*')x++;
if(j<m-1)
if(a[j+1]=='*')x++;
return (char)('0'+x);
}
int main()
{
char a[150][150];
int m,n;
int no=1;
while(cin>>m>>n)
{
if(m==0&&n==0)break;
if(n>1)cout<<"\n";
for(int i=0;i<=m;i++)
for(int j=0;j<=n;j++)
a[j]='0';
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[j];
cout<<"Field #"<<no<<":\n";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
char t=check(a,i,j,m,n);
cout<<t;
}
cout<<"\n";
}
no++;
}
return 0;
}
PA. I don't know why
Posted: Mon Oct 27, 2008 9:26 pm
by hungkhtn
Sorry everybody. I am new user here. I've started Minesweeper, but by whatever reason the program results in Presentation Error. Could anybody spend a little time to figure it for me? It takes me one night, and I was tired because of this tricky mistake
Code: Select all
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
int numb(int i, int j, char str[1000][1000], int n, int m)
{
int r=0;
if (str[i][j]=='*') return -1;
for (int k=-1; k<=1; k++)
for (int h=-1; h<=1; h++)
if (k!=0|| h!=0)
{
int px=i+k;
int py=j+h;
if (px>=0&&px<n && py>=0&&py<=m && str[px][py]=='*') r++;
}
return r;
}
void process(int a, char str[1000][1000], int n, int m)
{
cout<<"Field #"<<a<<": "<<endl;
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
int k=numb(i,j,str,n,m);
if (k==-1) cout<<"*"; else cout<<k;
}
cout<<endl;
}
}
int main(int argc, char *argv[])
{
int numf=1;
int m,n;
while (true)
{
cin>>n>>m;
char str[1000][1000];
if (m==0 && n==0) return 1;
for (int i=0; i<n; i++)
{
cin>>str[i];
}
process(numf, str,n,m);
numf++;
cout<<endl;
}
return 1;
}
Re: 10189 - Minesweeper
Posted: Mon Oct 27, 2008 9:27 pm
by hungkhtn
I am so appreciated if someone can help me.
Re: 10189 - Minesweeper
Posted: Wed Nov 12, 2008 11:04 am
by balladheart
help please why do I get WA T_T
Code: Select all
#include <iostream>
using namespace std;
char arena[101][101];
void clearArena(int m, int n);
void setArena(int m, int n);
void printArena(int m, int n);
void countMines(int m, int n);
int main(){
int m, n, count = 1;
while(1){
cin >> m >> n;
if(count != 1)cout << endl;
if(m == 0 || n == 0)
break;
clearArena(m,n);
setArena(m,n);
countMines(m,n);
cout << "Field #" << count << ":\n";
count++;
printArena(m,n);
}
return 0;
}
void countMines(int m, int n){
int x, y, tempX, tempY;
int count;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(arena[i][j] == '*')
continue;
count = 0;
x = j - 1;
y = i - 1;
for(int a = 0; a < 3; a++){
tempY = y + a;
if(tempY < 0 || tempY >= m)
continue;
for(int b = 0; b < 3; b++){
tempX = x + b;
if(tempX < 0 || tempX >= n)
continue;
if(arena[tempY][tempX] == '*')
count++;
}
}
arena[i][j] = '0' + count;
}
}
}
void setArena(int m, int n){
for(int i = 0; i < m; i++){
cin >> arena[i];
}
}
void clearArena(int m, int n){
m++;
n++;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
arena[i][j] = '\0';
}
void printArena(int m, int n){
for(int i = 0; i < m; i++)
cout << arena[i] << endl;
//cout << "\n";
}
Re: 10189 - Minesweeper
Posted: Wed Nov 12, 2008 8:47 pm
by mangoo
Hello to all,
i'm getting wa. after trying all the possible input cases i couldn't find what's wrong.
i've tried all the inputs in the previous posts. all outputs are ok.
i've tried hours.
i'm wondering what the program should do when either m or n is 0.
because the problem says to terminate only when m=n=0.
here's my code:
Code: Select all
#include<iostream>
#include<string>
using namespace std;
int main(void){
long M,N;
long cases=0;
while(cin>>M>>N) { char Field[1000][102];
if(M==0 && N==0) break;
cases++;
//TAKING INPUT IN M ROWS AND N COLUMNS
long l;
for(l=0;l<M;l++) {
long k;
for(k=0;k<N;k++)
cin>>Field[l][k];
}
if(cases>1) cout<<endl;
cout<<"Field #"<<cases<<endl;
//CALCULATING MINES
for(long m=0;m<M;m++){
for(long n=0;n<N;n++) { int counter=0;
if(Field[m][n] != '*') {
for(long k=m-1;k<=m+1 ;k++)
for(long l=n-1;l<=n+1 ;l++)
if( k<M && k>=0 && l<N && l>=0 )
if(Field[k][l] == '*') counter++;
cout<<counter; }
else cout<<"*";
}
cout<<endl;
}
} //END OF WHILE LOOP
return 0;
}
Thanks in advance.
I really appreciate your help.

Re: 10189 - Minesweeper
Posted: Wed Nov 19, 2008 10:21 pm
by poixp
I think the problem is here:
Code: Select all
cout<<"Field #"<<cases<<endl;
//Should be:
cout<<"Field #"<<cases<<":"<< endl;
10189 - Minesweeper
Posted: Fri Jan 02, 2009 1:24 pm
by alirezanoori
Hey all,
I tested all of test data with my program and every one of them is correct!!! But I still get WA! I double checked each line of my code and found nothing wrong about it! Could you please take a look at it and find my bug please?
Thanks in advance. I appreciate your help.
10189 - Minesweeper
Posted: Sat Jan 17, 2009 10:20 pm
by aliahmed
W A. Someone please help me.
#include<stdio.h>
int main()
{
int n,m,i,j,count,f=0;
char sq[200][200];
count=1;
while(scanf("%d%d",&n,&m),n,m)
{
if(f==1)
printf("\n");
f=1;
for(i=0; i<n; i++)
{
for(j=0; j<=m; j++)
{
scanf("%c",&sq[j]);
if(sq[j]=='.')
sq[j]='0';
}
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(sq[j]=='*')
{
if(sq[i+1][j]>=48 && sq[i+1][j]<=57)
sq[i+1][j]++;
if(sq[i-1][j]>=48 && sq[i-1][j]<=57)
sq[i-1][j]++;
if(sq[j+1]>=48 && sq[j+1]<=57)
sq[j+1]++;
if(sq[j-1]>=48 && sq[j-1]<=57)
sq[j-1]++;
if(sq[i+1][j+1]>=48 && sq[i+1][j+1]<=57)
sq[i+1][j+1]++;
if(sq[i-1][j-1]>=48 && sq[i-1][j-1]<=57)
sq[i-1][j-1]++;
if(sq[i+1][j-1]>=48 && sq[i+1][j-1]<=57)
sq[i+1][j-1]++;
if(sq[i-1][j+1]>=48 && sq[i-1][j+1]<=57)
sq[i-1][j+1]++;
}
}
}
printf("Field #%d:",count++);
for(i=0; i<n; i++)
{
for(j=0; j<=m; j++)
{
printf("%c",sq[i][j]);
}
}
printf("\n");
}
return 0;
}