Hello this is my first post
I have been doing some of the problems on a site called programming challenges
<
http://www.programming-challenges.com> and I am stuck on one problem which is the minesweeper problem <
http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110102&format=html> I am getting the same output as the sample output but when I submit I get wrong answer, but I just don't know what im doing wrong, could it be the spacing between the grids?
here is my code in c++:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int field = 1;
int num1 = 0,num2 = 0;
bool edge = false;
string zstring = "";
vector<int>final;
while(cin>>num1>>num2)
if((num1 == 0)||(num2==0))
{
return 0;
}
{
string zstring = "";
final.resize(num1*num2);
for(int j=0; j < (final.size()); j++) // this fills in temp to equal zero for each part of array, checked to work
{
final[j]= 0;
}
for(int i = 0; i < num1; i++)// gets the input from user, checked it works
{
string temp = "";
cin >> temp;
zstring += temp.substr(0,num2);
}
for(int x = 0; x < (num1*num2); x++)// main check loop
{
edge = false;
if(x < num2)//checks for top outer edges,works
{
edge = true;
if((zstring[x]) == (char)298)
{
if((x - 1) < 0)// if in top left hand corner
{
final[x+1] += 1;
final[x+num2] += 1;
final[x+num2+1] += 1;
}
else if(x == (num2-1))// checks if top right hand corner
{
final[x-1] += 1;
final[x+num2] += 1;
final[x+num2-1] += 1;
}
else // if middle top
{
final[x+1] += 1;
final[x-1] += 1;
final[x+num2] += 1;
final[x+num2+1] += 1;
final[x+num2-1] += 1;
}
}
}
else if(x >= ((num1-1)*num2))//checks for bottom outer edges,works
{
edge = true;
if((zstring[x]) == (char)298)
{
if((x + 1) >= (num1*num2))// if in bottom right hand corner
{
final[x-1] += 1;
final[x-num2] += 1;
final[(x-num2)-1] += 1;
}
else if(x == ((num1-1)*num2))// checks if bottom left hand corner
{
final[x+1] += 1;
final[x-num2] += 1;
final[(x-num2)+1] += 1;
}
else // if middle bottom
{
final[x+1] += 1;
final[x-1] += 1;
final[x-num2] += 1;
final[(x-num2)+1] += 1;
final[(x-num2)-1] += 1;
}
}
}
else
{
edge = false;
for(int b = 1; b <= num1; b++)
{
if(x == b*num2)
{
if((zstring[x]) != (char)298)// if on left side inner edge
{
}
else
{
final[x-num2]+=1;
final[x+num2]+=1;
final[x+1]+=1;
final[(x-num2+1)]+=1;
final[(x+num2+1)]+=1;
}
edge = true;
break;
}
else if(x == b*num2+(num2-1))// if on right size inner edge
{
if((zstring[x]) != (char)298)
{
}
else
{
final[x-num2]+=1;
final[x+num2]+=1;
final[x-1]+=1;
final[(x-num2-1)]+=1;
final[(x+num2-1)]+=1;
}
edge = true;
break;
}
} //end of for loop
}//end of else
if((zstring[x] == (char)298)&& (edge == false))// if a mine and not on the edge at all
{
final[x-num2]+=1;
final[x+num2]+=1;
final[x-1]+=1;
final[x+1]+=1;
final[(x-num2)+1]+=1;
final[(x-num2)-1]+=1;
final[(x+num2)+1]+=1;
final[(x+num2)-1]+=1;
}
}//end of main check loop
cout << "Field #" << field << ":" << endl;
for(int z = 0; z < (num1*num2); z++)// displays final
{
if(zstring[z] == (char)298)
{
cout << "*";
}
else
{
cout << final[z];
}
for(int y = 1; y <= num1; y++)
{
if(z == (y*num2)-1)// ends line when needed for display of final
{
cout << endl;
}
}
}// end of final display
cout << endl;
field += 1;
for(int j=0; j < (final.size()); j++) // this fills in temp to equal zero for each part of array, checked to work
{
final[j]= 0;
}
}// end of get input
return 0;
}
end of code
If anyone has done this challenge before or it is something obvious could you please help, and don't worry about how efficient the code is because im only a beginner(sort of)
any help appreciated thanks