UVA 10189 help

I tried to solve UVA problem 10189 and the test cases were all correct but for some reason, it is still marked wrong. Can someone please check the code?

Thanks in advance for help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>


using namespace std;



int main()
{
	static int cycles=1;
	 int rows, columns; 
	while (cin>> rows)
	{
		cin >> columns;
		if (rows==0 && columns==0)
		{
			return 0;
		}
		char safe='.';
		char unsafe='*';

		bool ismine[rows][columns];
		int field[rows][columns];
	

		char temp; 
		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				cin>>temp; 
				if(temp==safe)
				{
					ismine[i][j]=false;
				}
				else if (temp==unsafe)
				{
					ismine[i][j]=true; 
				}
				field[i][j]=0;
			}
		}


		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				if(ismine[i][j]==true)
				{
					field[i][j]=-1;

					if(j<columns-1 && field[i][j+1]!=-1)
					{
						field[i][j+1]++;
					}
					if(j>0 && field[i][j-1]!=-1)
					{
							field[i][j-1]++;
					}
					if (i>0 && field[i-1][j]!=-1)
					{
							field[i-1][j]++;
					}
					if (i<rows-1 && field[i+1][j]!=-1)
					{
							field[i+1][j]++;
					}
					if (j<columns-1 && i<rows-1 && field[i+1][j+1]!=-1)
					{
							field[i+1][j+1]++;
					}
					if(j<columns-1 && i>0 && field[i-1][j+1]!=-1)
					{
							field[i-1][j+1]++;
					}
					if(j>0 && i<rows-1 && field[i+1][j-1] !=-1)
					{
							field[i+1][j-1]++;
					}
					if (j>0 && i>0 && field[i-1][j-1] !=-1)
					{ 
							field[i-1][j-1]++;
					}
				}	
			}
		}

		cout << endl;
		cout << "Field #" << cycles << ":" << endl;
		cycles++;

		for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				if(field[i][j]==-1)
				{
					cout << "*";
				}
				else
				{
					cout << field[i][j];
				}
			}
			cout << endl; 
		}

	}
}
What exactly UVA. Post a link. How it is "wrong"? Compiler error, runtime error, incorrect answer?

1
2
bool ismine[rows][columns];
int field[rows][columns];
It is not valid C++ construction. Probably your problem lies here.
> the test cases were all correct but for some reason, it is still marked wrong
The sample input is not the only test case.
Also, they were not, you start with a line break.

By the way, 53--84 is error prone.
Last edited on
What do you mean by error prone? Would love to hear some feedback. Also should I change this?
1
2
bool ismine[rows][columns];
int field[rows][columns];


Also I tested other test cases from different websites. A google search gives a few test cases
Last edited on
> What do you mean by error prone?
Your approach is simple, if you reach a mine you increase the neighbours counter.
The problem is your implementation, you need to make sure that:
- you don't have out of bounds access (your conditionals)
- you are actually touching what you want (the conditional and the statement refer to the same cell)
- you are visiting all the neighbours, and only one time

That verification takes time, and you may made mistakes doing it. So you may end with something incorrect that is reviewed as right.


> Also I tested other test cases from different websites
¿didn't you notice an extra blank line at the beginning of the output?
By instance, for
1 1
.
0 0
it should be
Field #1:
0
not
Field #1:
0

not

Field #1:
0



About your arrays declaration, what you're doing is illegal, so yes, consider to change it.
i changed it already. Thanks ne555, but it still says wrong answer.

Can you please help my identify which portion of the code is the wrong one?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

for (int i=0; i<rows; i++)
		{
			for (int j=0; j<columns; j++)
			{
				if(ismine[i][j]==true)
				{
					field[i][j]=-1;

					if(j<columns-1 && field[i][j+1]!=-1)
					{
						field[i][j+1]++;
					}
					if(j>0 && field[i][j-1]!=-1)
					{
							field[i][j-1]++;
					}
					if (i>0 && field[i-1][j]!=-1)
					{
							field[i-1][j]++;
					}
					if (i<rows-1 && field[i+1][j]!=-1)
					{
							field[i+1][j]++;
					}
					if (j<columns-1 && i<rows-1 && field[i+1][j+1]!=-1)
					{
							field[i+1][j+1]++;
					}
					if(j<columns-1 && i>0 && field[i-1][j+1]!=-1)
					{
							field[i-1][j+1]++;
					}
					if(j>0 && i<rows-1 && field[i+1][j-1] !=-1)
					{
							field[i+1][j-1]++;
					}
					if (j>0 && i>0 && field[i-1][j-1] !=-1)
					{ 
							field[i-1][j-1]++;
					}
				}	
			}
		}
As I said, that task is difficult and time consuming, so I rather don't do it.
Try to rewrite that portion so it is easier to analyze.


> i changed it already. Thanks ne555, but it still says wrong answer.
I changed it and got `Accepted'
Topic archived. No new replies allowed.