10279 - Mine Sweeper
Moderator: Board moderators
-
- New poster
- Posts: 1
- Joined: Fri Aug 03, 2012 4:56 pm
- Location: Chittagong University of Engineering and Technology
- Contact:
Re: 10279 - Mine Sweeper
Check this line : "If a mine has been touched, all positions with a mine should contain an asterisk".
input :
1
3
.*.
..*
..*
xx.
...
...
output :
1*.
..*
..*
input :
1
3
.*.
..*
..*
xx.
...
...
output :
1*.
..*
..*
Re: 10279 - Mine Sweeper
WA.please help
Code: Select all
Removed after ACCEPTED
i am getting WA at uva=10279(Mine Sweeper)
# include <iostream>
# include <cstdio>
using namespace std;
int cx[]={-1,-1,-1,0,0,1,1,1};
int cy[]={-1,0,1,-1,1,-1,0,1};
char g[100][100];int n,m;
int DFS(int x,int y){
int cnt=0;
for(int i=0;i<8;i++){
if(g[x+cx][y+cy]=='*'){
cnt++;
}
}
return cnt;
}
int main (){
int i,j,k=1;
bool flag=false;
while(cin>>n>>m){
if(!n && !m) break;
if(flag)cout<<endl;flag=true;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>g[j];
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(g[j]!='*' && g[j]=='.'){
g[j]=(DFS(i,j)+'0');
}
}
}
cout<<"Field #"<<k++<<":"<<endl;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<g[j];
}
cout<<endl;
}
}
return 0;}
# include <cstdio>
using namespace std;
int cx[]={-1,-1,-1,0,0,1,1,1};
int cy[]={-1,0,1,-1,1,-1,0,1};
char g[100][100];int n,m;
int DFS(int x,int y){
int cnt=0;
for(int i=0;i<8;i++){
if(g[x+cx][y+cy]=='*'){
cnt++;
}
}
return cnt;
}
int main (){
int i,j,k=1;
bool flag=false;
while(cin>>n>>m){
if(!n && !m) break;
if(flag)cout<<endl;flag=true;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>g[j];
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(g[j]!='*' && g[j]=='.'){
g[j]=(DFS(i,j)+'0');
}
}
}
cout<<"Field #"<<k++<<":"<<endl;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cout<<g[j];
}
cout<<endl;
}
}
return 0;}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: i am getting WA at uva=10279(Mine Sweeper)
line 12 should be
if(x+cx>=0 && y+cy>=0 && x+cx<n && y+cy<m && g[x+cx][y+cy]=='*'){
if(x+cx>=0 && y+cy>=0 && x+cx<n && y+cy<m && g[x+cx][y+cy]=='*'){
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
I kept getting WA on this problem.
Can someone help me?
Thanks in advance.![:)](./images/smilies/icon_smile.gif)
Can someone help me?
Thanks in advance.
![:)](./images/smilies/icon_smile.gif)
Code: Select all
Thanks, Brianfry.
I got AC.
Last edited by dibery on Sun Jul 21, 2013 6:45 pm, edited 1 time in total.
Life shouldn't be null.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10279 - Mine Sweeper
Don't print null chars.
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
Why WA??![]()
![]()
Code: Select all
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000][1000],ss[1000][1000],ch;
long long int n,m,i,j,x,y,i1,j1,t,T,C;
scanf("%lld",&T);printf("\n");
for(t=1;t<=T;)
{
C=0;
scanf("%lld",&n);m=n;
for(i=0;i<n;i++){for(j=0;j<m;){scanf("%c",&ch);if(ch=='*'||ch=='.') { s[i][j]=ch; if(ch=='.') {s[i][j]='0';} j++; }}}i=0;j=0;
for(i=0;i<n;i++){for(j=0;j<m;){scanf("%c",&ch);if(ch=='x'||ch=='.') { ss[i][j]=ch; if(ch=='.') {ss[i][j]='0';} j++; }}}i=0;j=0;
for(x=0;x<n;x++)
{
for(y=0;y<m;y++)
{
if(s[x][y]=='*')
{
if(x!=0 && y!=0 && x!=n-1&&y!=m-1)
{
if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1; }
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;}
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==0 && y!=0 && y!=m-1)
{
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1; }
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1; }
if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==n-1 && y!=0 && y!=m-1)
{
if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1; }
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
}
else if(y==0 && x!=0&& x!=n-1)
{
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1; }
if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(y==m-1 && x!=n-1&&x!=0)
{
if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1;}
if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
}
else if(x==0&&y==0)
{
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
if(s[x+1][y+1]!='*') {s[x+1][y+1]=s[x+1][y+1]+1;}
}
else if(x==n-1&&y==0)
{
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x-1][y+1]!='*') {s[x-1][y+1]=s[x-1][y+1]+1;}
if(s[x][y+1]!='*') {s[x][y+1]=s[x][y+1]+1;}
}
else if(x==0&&y==m-1)
{
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1; }
if(s[x+1][y-1]!='*') {s[x+1][y-1]=s[x+1][y-1]+1;}
if(s[x+1][y]!='*') {s[x+1][y]=s[x+1][y]+1;}
}
else if(x==n-1&&y==m-1)
{
if(s[x-1][y-1]!='*') {s[x-1][y-1]=s[x-1][y-1]+1;}
if(s[x-1][y]!='*') {s[x-1][y]=s[x-1][y]+1;}
if(s[x][y-1]!='*') {s[x][y-1]=s[x][y-1]+1; }
}
}
}
}
for(i=0;i<n;i++){for(j=0;j<m;j++){ if(ss[i][j]=='x') {ss[i][j]=s[i][j];if(s[i][j]=='*') {C=1;}} else ss[i][j]='.'; }}i=0;j=0;
if(C==1) { for(i=0;i<n;i++){for(j=0;j<m;j++){ if(s[i][j]=='*') {ss[i][j]='*';} }} }i=0;j=0;
for(i=0;i<n;i++){for(j=0;j<m;j++){ printf("%c",ss[i][j]); }printf("\n");}i=0;j=0;
if(t!=T)printf("\n");
t++;}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10279 - Mine Sweeper
Don't print a blank line at the start of the output.
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
What's the problem....?
Code: Select all
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define zero(arr) memset(arr,0,sizeof(arr));
#define rep(i,n) for(int i=0;i<n;i++)
#define FOR(i,n,k) for(int i=0;i<n;i=i+k)
#define VI vector<int>
#define VVI vector< VI >
#define VIit VI::iterator
#define pb(a) push_back(a)
#define SI(a) scanf("%d",&a)
#define SU(a) scanf("%u",&a)
#define SHD(a) scanf("%hd",&a)
#define SHU(a) scanf("%hu",&a)
#define SLLD(a) scanf("%lld",&a)
#define SLLU(a) scanf("%llu",&a)
#define SF(a) scanf("%f",&a)
#define SLF(a) scanf("%lf",&a)
#define SC(a) scanf("%c",&a)
#define SS(a) scanf("%s",a)
using namespace std;
int dirR8[]= {0,-1,-1,-1,0,1,1,1};
int dirC8[]= {1,1,0,-1,-1,-1,0,1};
int dirR4[]= {0,-1,0,1};
int dirC4[]= {1,0,-1,0};
char grid1[11][11];
char grid2[11][11];
int cnt;
int main()
{
int cases;
SI(cases);
int sp=0;
while(cases--)
{
int flag=0;
int rc;
if(sp)cout<<endl;
sp=1;
SI(rc);
getchar();
rep(i,rc)gets(grid1[i]);
rep(i,rc)rep(j,rc){
cin>>grid2[i][j];
if(grid2[i][j]=='x' && grid1[i][j]=='*'){flag=1;grid2[i][j]='.';}
}
rep(i,rc)rep(j,rc)if(grid2[i][j]=='x')
{
cnt=0;
rep(k,8)
{
int R=i+dirR8[k];
int C=j+dirC8[k];
if(R>=0 && C>=0 && R<rc && C<rc && grid1[R][C]=='*' )
{
cnt++;
}
}
if(flag==0)grid2[i][j]=cnt+'0';
else grid1[i][j]=cnt+'0';
}
if(flag==0) rep(i,rc)printf("%s\n",grid2[i]);
else rep(i,rc)printf("%s\n",grid1[i]);
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10279 - Mine Sweeper
Input:AC output:
Code: Select all
3
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxx.....
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxXX...
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.x...
xxxxx...
Code: Select all
001.....
0013....
0001....
00011...
00001...
00123...
001.....
001.....
001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...
001**..*
0013..*.
0001*...
00011...
00001...
00123*..
001**.*.
00123*..
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
Successfully pass your test cases...Still WA.....
Code: Select all
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define zero(arr) memset(arr,0,sizeof(arr));
#define rep(i,n) for(int i=0;i<n;i++)
#define FOR(i,n,k) for(int i=0;i<n;i=i+k)
#define VI vector<int>
#define VVI vector< VI >
#define VIit VI::iterator
#define pb(a) push_back(a)
#define SI(a) scanf("%d",&a)
#define SU(a) scanf("%u",&a)
#define SHD(a) scanf("%hd",&a)
#define SHU(a) scanf("%hu",&a)
#define SLLD(a) scanf("%lld",&a)
#define SLLU(a) scanf("%llu",&a)
#define SF(a) scanf("%f",&a)
#define SLF(a) scanf("%lf",&a)
#define SC(a) scanf("%c",&a)
#define SS(a) scanf("%s",a)
using namespace std;
int dirR8[]= {0,-1,-1,-1,0,1,1,1};
int dirC8[]= {1,1,0,-1,-1,-1,0,1};
int dirR4[]= {0,-1,0,1};
int dirC4[]= {1,0,-1,0};
char grid1[11][11];
char grid2[11][11];
int cnt;
int main()
{
// freopen("in.txt","r",stdin);
int cases;
SI(cases);
int sp=0;
while(cases--)
{
int flag=0;
int rc;
if(sp)printf("\n");
sp=1;
SI(rc);
getchar();
rep(i,rc)gets(grid1[i]);
rep(i,rc)rep(j,rc){
cin>>grid2[i][j];
if(grid2[i][j]!='.' && grid1[i][j]=='*'){flag=1;grid2[i][j]='.';}
}
rep(i,rc)rep(j,rc)if(grid2[i][j]!='.')
{
cnt=0;
rep(k,8)
{
int R=i+dirR8[k];
int C=j+dirC8[k];
if(R>=0 && C>=0 && R<rc && C<rc && grid1[R][C]=='*' )
{
cnt++;
}
}
if(flag==0)grid2[i][j]=cnt+'0';
else grid1[i][j]=cnt+'0';
}
if(flag==0) rep(i,rc)printf("%s\n",grid2[i]);
else rep(i,rc)printf("%s\n",grid1[i]);
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10279 - Mine Sweeper
Change the way you parse the input. Try:AC output:
Code: Select all
1
8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxxxxxxx
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...
Code: Select all
001**22*
0013..*.
0001*...
00011...
00001...
00123*..
001**.*.
00123*..
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
Plz help me. I cant understand why its WA?
Code: Select all
#include<stdio.h>
int main()
{
int m, n, i, j, cnt[150], pnt, ans[11][11], k, l, flg;
char str[11][11], prnt[11][11];
scanf("%d", &n);
while( n-- )
{
printf("\n");
scanf("%d",&m);
for( i=0; i<m; i++ )
scanf("%s",str[i]);
for( i=0; i<m; i++ )
scanf("%s",prnt[i]);
flg=0;
pnt = 0;
for( i=0; i<m; i++ )
{
for( j=0; j<m; j++ )
{
if( str[i][j] == '*' )
{
ans[i][j] = -1;
cnt[pnt] = i;
pnt++;
cnt[pnt] = j;
pnt++;
if( prnt[i][j] == 'x' || prnt[i][j] == 'X' )
flg=1;
}
else
ans[i][j] = 0;
}
}
for( i=0,j=1; j<pnt; i=i+2,j=j+2 )
{
k = cnt[i];
l = cnt[j];
if( (k-1)>=0 && (l-1)>=0 && ans[k-1][l-1]!=-1 )
ans[k-1][l-1]++;
if( (k-1)>=0 && ans[k-1][l]!=-1 )
ans[k-1][l]++;
if( (k-1)>=0 && (l+1)<m && ans[k-1][l+1]!=-1 )
ans[k-1][l+1]++;
if( (l-1)>=0 && ans[k][l-1]!=-1 )
ans[k][l-1]++;
if( (l+1)<m && ans[k][l+1]!=-1 )
ans[k][l+1]++;
if( (k+1)<m && (l-1)>=0 && ans[k+1][l-1]!=-1 )
ans[k+1][l-1]++;
if( (k+1)<m && ans[k+1][l]!=-1 )
ans[k+1][l]++;
if( (k+1)<m && (l+1)<m && ans[k+1][l+1]!=-1 )
ans[k+1][l+1]++;
}
for( i=0; i<m; i++ ) {
for( j=0; j<m; j++ ) {
if( prnt[i][j] == 'x' || prnt[i][j] == 'X' ) {
if( ans[i][j] != -1 )
printf("%d",ans[i][j]);
else
printf("*");
}
else {
if( ans[i][j]==-1 && flg==1 )
printf("*");
else
printf(".");
}
}
printf("\n");
}
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10279 - Mine Sweeper
Don't print a blank line at the start of the output.
Check input and AC output for thousands of problems on uDebug!
Re: 10279 - Mine Sweeper
Thnx. I got AC.