10189 - Minesweeper
Moderator: Board moderators
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
Re: 10189 - Minesweeper
I am getting wrong answer though the code is showing correct output for sample input......Is the extra new line at the end creating problem ? If i have to remove it then how ?
but the problem says : " There must be an empty line between field outputs. "
problem : http://uva.onlinejudge.org/index.php?op ... oblem=1130
my code : http://pastebin.com/d8inZWWJ#
but the problem says : " There must be an empty line between field outputs. "
problem : http://uva.onlinejudge.org/index.php?op ... oblem=1130
my code : http://pastebin.com/d8inZWWJ#
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
line 14 of your code won't compile.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
Re: 10189 - Minesweeper
@ sir Brianfry713,,,
sir, the actual code that i compiled in my compiler was not that...there was an error (.) but it happened as i just copied the whole code at pastebin.com and then deleted my unnecessary comments there....my actual code is ( http://pastebin.com/ia6hZJbQ) .....which is working accurately for sample inputs !!!!
sir, the actual code that i compiled in my compiler was not that...there was an error (.) but it happened as i just copied the whole code at pastebin.com and then deleted my unnecessary comments there....my actual code is ( http://pastebin.com/ia6hZJbQ) .....which is working accurately for sample inputs !!!!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
There must be an empty line between field outputs. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
10189 - Minesweeper
" There must be an empty line between field outputs. Don't print an extra blank line at the end. "
But how to do that...????? I am taking input from keyboard n when 0 0 is pressed,,the programme terminates.....how i can understand the number of input cases there ??
But how to do that...????? I am taking input from keyboard n when 0 0 is pressed,,the programme terminates.....how i can understand the number of input cases there ??
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
It's easier to put the input into a file and redirect it into your program. In UNIX I usually do something like:
./a.out < 10189.in | diff - 10189.out
To print a newline between test cases you could do something like:
./a.out < 10189.in | diff - 10189.out
To print a newline between test cases you could do something like:
Code: Select all
for(cn = 1; scanf("%d%d", &n, &m) == 2 && n; cn++) {
if(cn > 1)
puts("");
// the rest of your code goes here
}
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
Re: 10189 - Minesweeper
@brainfry713
////////****[It's easier to put the input into a file and redirect it into your program. In UNIX I usually do something like:
./a.out < 10189.in | diff - 10189.out
] ********/////// ** I didnt understand it ?
////****[To print a newline between test cases you could do something like:
for(cn = 1; scanf("%d%d", &n, &m) == 2 && n; cn++) { /////// why also n ????????/
if(cn > 1)
puts(""); ////////// shouldnt it be "\n"or will puts(""); work the same !!!
// the rest of your code goes here
}
]******////////
////////****[It's easier to put the input into a file and redirect it into your program. In UNIX I usually do something like:
./a.out < 10189.in | diff - 10189.out
] ********/////// ** I didnt understand it ?
////****[To print a newline between test cases you could do something like:
for(cn = 1; scanf("%d%d", &n, &m) == 2 && n; cn++) { /////// why also n ????????/
if(cn > 1)
puts(""); ////////// shouldnt it be "\n"or will puts(""); work the same !!!
// the rest of your code goes here
}
]******////////
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
Re: 10189 - Minesweeper
This time i am not printing an extra line at the end....Though i am getting wrong answer !!!!!!!
http://pastebin.com/u2ttD3w6
http://pastebin.com/u2ttD3w6
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
In UNIX '<' redirects a file to stdin, '|' redirects the output to the next command, '-' redirects the previous output to an argument.
In my for loop I check n to see if it's non-zero.
puts(""); is the same as printf("\n");
You code works on the sample input on my machine, but you're not checking array boundaries.
if i and j are zero you should not be reading a[j - 1]
In my for loop I check n to see if it's non-zero.
puts(""); is the same as printf("\n");
You code works on the sample input on my machine, but you're not checking array boundaries.
if i and j are zero you should not be reading a[j - 1]
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 9
- Joined: Sun Jul 21, 2013 2:34 pm
Re: 10189 - Minesweeper
I got accepted........Thanks BRIAN for helping me a lot.......
Threats :
1) we have to print newlines between consecutive outputs...not an extra newline at the end..
/*
1 1
.
Field #1:
0
1 1
// newline
.
Field #2:
0
0 0
// newline by default ( we must not print an extra new line here same as the newline before )
Process returned 0
*/
2) we may not declare the variables as long int....only "int" is enough
3) though we may get correct output for sample input......but to get accepted verdict, we have to change the conditions from if(a[i-1][j-1]=='*') b[j]++; TO >>>>>
if((i-1>=0&&j-1>=0)&&(a[i-1][j-1]=='*')) b[j]++;
Threats :
1) we have to print newlines between consecutive outputs...not an extra newline at the end..
/*
1 1
.
Field #1:
0
1 1
// newline
.
Field #2:
0
0 0
// newline by default ( we must not print an extra new line here same as the newline before )
Process returned 0
*/
2) we may not declare the variables as long int....only "int" is enough
3) though we may get correct output for sample input......but to get accepted verdict, we have to change the conditions from if(a[i-1][j-1]=='*') b[j]++; TO >>>>>
if((i-1>=0&&j-1>=0)&&(a[i-1][j-1]=='*')) b[j]++;
Re: 10189 - Minesweeper
I cant' for the life of me, find what is wrong with my code below.
Any hints from experienced programmers out there?
Any hints from experienced programmers out there?
Code: Select all
#include <iostream>
using namespace std;
int main()
{
int m = 0, n = 0;
int iter = 1;
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ctr = 0;
char buffer = ' ';
char board[100][100];
while(cin >> m >> n)
{
if(m == 0 && n == 0)
break;
if(iter > 1)
cout << endl << endl;
cout << "Field #" << iter << ":" << endl;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> buffer;
board[i][j] = buffer;
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(board[i][j] != '*')
{
ctr = 0;
for(int q = 0; q < 8; q++)
if((dx[q] + i >= 0 && dx[q] + i < m) && (dy[q] + j >= 0 && dy[q] + j < n))
if(board[dx[q] + i][dy[q] + j] == '*')
ctr++;
cout << ctr;
}
else
cout << "*";
}
if(i + 1 < m)
cout << endl;
}
iter++;
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
Print a newline at the end of the last line.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 6
- Joined: Tue Nov 12, 2013 5:57 pm
Re: 10189 - Minesweeper
I used all line formats, but still getting WA ,,,,
here is my code..... help .....
here is my code..... help .....
Code: Select all
#include<iostream>
using namespace std;
int main()
{
int n,m,i,j,x=0;
cin>>n>>m;
while(n&&m)
{
x++;
char **a=new char*[n];
for(i=0;i<n;i++)
a[i]=new char[m];
char **b=new char*[n];
for(i=0;i<n;i++)
b[i]=new char[m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>a[i][j];
b[i][j]=a[i][j];
}
/* for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
*/
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]=='*')
{
b[i][j]='*';
if((i-1>=0)&&(j-1>=0))
{
if(b[i-1][j-1]>='0')
b[i-1][j-1]+=1;
else if(b[i-1][j-1]=='.')
b[i-1][j-1]='1';
}
if((i-1>=0)&&(j>=0))
{
if(b[i-1][j]>='0')
b[i-1][j]+=1;
else if(b[i-1][j]=='.')
b[i-1][j]='1';
}
if((i-1>=0)&&(j+1<m))
{
if(b[i-1][j+1]>='0')
b[i-1][j+1]+=1;
else if(b[i-1][j+1]=='.')
b[i-1][j+1]='1';
}
if((i<n)&&(j+1<m))
{
if(b[i][j+1]>='0')
b[i][j+1]+=1;
else if(b[i][j+1]=='.')
b[i][j+1]='1';
}
if((i+1<n)&&(j+1<m))
{
if(b[i+1][j+1]>='0')
b[i+1][j+1]+=1;
else if(b[i+1][j+1]=='.')
b[i+1][j+1]='1';
}
if((i+1<n)&&(j<m))
{
if(b[i+1][j]>='0')
b[i+1][j]+=1;
else if(b[i+1][j]=='.')
b[i+1][j]='1';
}
if((i+1<n)&&(j-1>=0))
{
if(b[i+1][j-1]>='0')
b[i+1][j-1]+=1;
else if(b[i+1][j-1]=='.')
b[i+1][j-1]='1';
}
if((i>=0)&&(j-1>=0))
{
if(b[i][j-1]>='0')
b[i][j-1]+=1;
else if(b[i][j-1]=='.')
b[i][j-1]='1';
}
}
}
}
cout<<"Field #"<<x<<":\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[i][j]=='.')
cout<<"0";
else
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"\n";
delete a;
delete b;
cin>>n>>m;
}
cout<<"\n";
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10189 - Minesweeper
There must be an empty line between field outputs. You're printing extra blank lines at the end.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 6
- Joined: Tue Nov 12, 2013 5:57 pm
Re: 10189 - Minesweeper
I removed the last blank line from the code,, still shows WA.....
Code: Select all
#include<iostream>
using namespace std;
int main()
{
int n,m,i,j,x=0;
cin>>n>>m;
while(n&&m)
{
x++;
char **a=new char*[n];
for(i=0;i<n;i++)
a[i]=new char[m];
char **b=new char*[n];
for(i=0;i<n;i++)
b[i]=new char[m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>a[i][j];
b[i][j]=a[i][j];
}
/* for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
*/
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]=='*')
{
b[i][j]='*';
if((i-1>=0)&&(j-1>=0))
{
if(b[i-1][j-1]>='0')
b[i-1][j-1]+=1;
else if(b[i-1][j-1]=='.')
b[i-1][j-1]='1';
}
if((i-1>=0)&&(j>=0))
{
if(b[i-1][j]>='0')
b[i-1][j]+=1;
else if(b[i-1][j]=='.')
b[i-1][j]='1';
}
if((i-1>=0)&&(j+1<m))
{
if(b[i-1][j+1]>='0')
b[i-1][j+1]+=1;
else if(b[i-1][j+1]=='.')
b[i-1][j+1]='1';
}
if((i<n)&&(j+1<m))
{
if(b[i][j+1]>='0')
b[i][j+1]+=1;
else if(b[i][j+1]=='.')
b[i][j+1]='1';
}
if((i+1<n)&&(j+1<m))
{
if(b[i+1][j+1]>='0')
b[i+1][j+1]+=1;
else if(b[i+1][j+1]=='.')
b[i+1][j+1]='1';
}
if((i+1<n)&&(j<m))
{
if(b[i+1][j]>='0')
b[i+1][j]+=1;
else if(b[i+1][j]=='.')
b[i+1][j]='1';
}
if((i+1<n)&&(j-1>=0))
{
if(b[i+1][j-1]>='0')
b[i+1][j-1]+=1;
else if(b[i+1][j-1]=='.')
b[i+1][j-1]='1';
}
if((i>=0)&&(j-1>=0))
{
if(b[i][j-1]>='0')
b[i][j-1]+=1;
else if(b[i][j-1]=='.')
b[i][j-1]='1';
}
}
}
}
cout<<"Field #"<<x<<":\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[i][j]=='.')
cout<<"0";
else
cout<<b[i][j];
}
cout<<"\n";
}
cout<<"\n";
delete a;
delete b;
cin>>n>>m;
}
return 0;
}