Re: 10279 - Mine Sweeper
Posted: Tue Aug 12, 2014 8:57 pm
Removed after accepted ! 

Yes that is a mistake. You should check that the array location is valid. Don't count on getting RE in C++.Shahidul.CSE wrote:Here I checked in[i+1].ch[j+1] and in[i-1].ch[j-1], even when they does not exist.(i.e. i or j equal to 0, n; Is this a mistake? But I checked all the test cases, I am getting correct output. For this checking program should get RE, not WA. Am I right?
So, is there anything going wrong with my code? Please help me to find the bug.
Code: Select all
AC
Code: Select all
AC
sohel wrote:You have to print a blank line between two consecutive outputs.
Code: Select all
if(check)
printf("\n");
check = true;
You didn't completely performed Brianfry's advice. Your checkings j < n and i < n are not correct. One example how it should bebrianfry713 wrote:Don't read and write outside of array boundaries.
Code: Select all
(board[i][j+1] < bomb && j + 1 < n) ? board[i][j+1]++: bomb;
Code: Select all
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
int m,n,h,i,j,k,l,s,q,cont,cont2;
while(scanf("%d",&m)==1){
for(h=1;h<=m;h++){
scanf("%d",&n);
char A[n][n],B[n][n],C[n][n];
for(i=0;i<n;i++){
cin>> A[i];
}
for(i=0;i<n;i++){
cin>> B[i];
}
cont2=0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(B[i][j]=='x' && A[i][j]=='*'){
cont2++;
}
if (B[i][j]=='x'){
k=i-1; l=i+1;
s=j-1; q=j+1;
if ( (k>=0 && k<n)&&(s>=0 && s<n) && A[k][s]=='*'){
cont++;
}
if ( (k>=0 && k<n) && A[k][j]=='*'){
cont++;
}
if ( (k>=0 && k<n)&&(q>=0 && q<n) && A[k][q]=='*'){
cont++;
}
if ( (s>=0 && s<n) && A[i][s]=='*'){
cont++;
}
if ( (q>=0 && q<n) && A[i][q]=='*'){
cont++;
}
if ( (l>=0 && l<n) && (s>=0 && s<n) && A[l][s]=='*'){
cont++;
}
if ( (l>=0 && l<n) && A[l][j]=='*'){
cont++;
}
if ( (l>=0 && l<n)&&(q>=0 && q<n) && A[l][q]=='*'){
cont++;
}
C[i][j]=(char)(((int)'0')+cont);
cont=0;
}
if(B[i][j]=='.'){
C[i][j]='.';
}
}
}
if(cont2>0){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(A[i][j]=='*'){
cout<<"*";
}
else
cout<<C[i][j];
}
printf("\n");
}
}
else{
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cout<<C[i][j];
}
printf("\n");
}
}
printf("\n");
}
}
return 0;
}
Code: Select all
*input*
5
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...
4
...*
.*..
....
....
xxx.
xxxx
xxxx
xxx.
2
.*
..
x.
x.
2
**
..
x.
xx
1
.
x
Code: Select all
*output*
001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...
112*
1*21
1110
000.
1.
1.
**
22
0