PDA

View Full Version : سوال: Minsweeper



طلیعه-988
پنج شنبه 28 مرداد 1389, 13:15 عصر
سلام.دوستان اگه ممکنه واسه سوال زیر کمکم کنید:
من کد مربوط به بازی Minesweeper رو نوشتم و تو سایت UVa online judge فرستادم و هی پیغام Wrong answer میده!!!هر کاری به ذهنم میومد کردم که دیگه به جایی قد نداد.
لطفا Help!!!



PC/UVa IDs:

110102/10189, Popularity: A, Success rate: high Level: 1

Have you ever played Minesweeper? This cute little game comes with a certain operating
system whose name we can’t remember. The goal of the game is to find where
all the mines are located within a


M × N field.

The game shows a number in a square which tells you how many mines there are
adjacent to that square. Each square has at most eight adjacent squares. The 4


×4 field

on the left contains two mines, each represented by a “


*” character. If we represent the

same field by the hint numbers described above, we end up with the field on the right:
*...
....
.*..
....
*100
2210
1*10
1110
Input
The input will consist of an arbitrary number of fields. The first line of each field
contains two integers


n and m (0 < n,m ≤ 100) which stand for the number of lines

and columns of the field, respectively. Each of the next


n lines contains exactly m

characters, representing the field.
Safe squares are denoted by “


.” and mine squares by “*,” both without the quotes.

The first field line where


n = m = 0 represents the end of input and should not be

processed.
Output
For each field, print the message


Field #x: on a line alone, where x stands for the

number of the field starting from 1. The next


n lines should contain the field with the




.” characters replaced by the number of mines adjacent to that square. There must

be an empty line between field outputs.
Sample Input
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
Sample Output
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100



#include<iostream>
#include<string>
using namespace std;
char field[110][110];
int FindNo(char[][110],int,int );
void Print(char [][110]);
int n,m;
int cnt=0;
int main()
{
int q=0;
while(cin>>n>>m && n!=0 && m!=0)
{
q++;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
field[i][j]=' ';
}
}
for(int i=0;i<n;i++) { cin>>field[i];}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(field[i][j]=='*')
continue;
else
{
cnt=FindNo(field,i,j);
field[i][j]=cnt;
cnt=0;
}
}
}
cout<<endl<<"Field #"<<q<<":"<<endl;
Print(field);
cout<<endl;
}
return 0;
}
int FindNo(char a[][110],int i,int j)
{
int J;
J=j;
for(int k=i-1;k<=i+1;k++)
{

for(int z=j-1;z<=j+1;z++)
{
if(field[k][z]=='*')
cnt++;
}
j=J;
}
return cnt;
}
void Print(char a[][110])
{
int ch;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
ch=(int)a[i][j];
if(ch==42)
{cout<<'*';}
else
{cout<<ch;}
ch=0;
}
cout<<endl;
}
}

woeful
جمعه 29 مرداد 1389, 10:01 صبح
سلام
کافی بین UsingNamespace یه فاصله بزاری using namespace
کد اجرا شد

طلیعه-988
شنبه 30 مرداد 1389, 18:21 عصر
کافی بین UsingNamespace یه فاصله بزاری using namespace
کد اجرا شد

فک میکنم این یه اشکال کپی پیستی بوده ،تو محیط ویژوال درست نوشتم.

runtime.error
دوشنبه 22 شهریور 1389, 18:19 عصر
دوست عزیز حل این سوال خیلی ساده است
من حال خوندن برنامتو نداشتم واسه همین واست نوشتم
این مطمئنا Accept میشه
البته الان که دارم فکر میکنم میبینم میشد خیلی بهتر نوشت. کافی بود طول و عرض آرایه رو 2 واحد بیشتر بگیریم تا یکم از بار پردازشی کم شه ( البته برای field های کوچک خیلی فرقی نداره)

/********************* In The Name Of God **********************
Project: Minesweeper
Author: Hamidreza Hosseinkhani (hosseinkhani@live.com)
************************************************** *************/
#include <iostream>
int main()
{
int n,m,x=0;
while(std::cin>>n>>m)
{
char ch;
x++;
if(n==0 && m==0) break;
char **field=new char *[n];
for(int i=0;i<n;i++)
{
field[i]=new char[m];
for(int j=0;j<m;j++)
{
std::cin>>ch;
if(ch=='.') field[i][j]='0';
else if(ch=='*') field[i][j]='*';
else std::cerr<<"Invalid Input";
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(field[i][j]=='*')
{
if(i>0)
{
if(j>0 && field[i-1][j-1]!='*')
field[i-1][j-1]++;
if(field[i-1][j]!='*')
field[i-1][j]++;
if(j<m-1 && field[i-1][j+1]!='*')
field[i-1][j+1]++;
}
if(i<n-1)
{
if(j>0 && field[i+1][j-1]!='*')
field[i+1][j-1]++;
if(field[i+1][j]!='*')
field[i+1][j]++;
if(j<m-1 && field[i+1][j+1]!='*')
field[i+1][j+1]++;
}
if(j>0 && field[i][j-1]!='*')
field[i][j-1]++;
if(j<m-1 && field[i][j+1]!='*')
field[i][j+1]++;
}
if(x!=1)
std::cout<<std::endl;
std::cout<<"Field #"<<x<<":\n";
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
std::cout<<field[i][j];
if((j+1)%m==0)
std::cout<<std::endl;
}
}
return 0;
}

طلیعه-988
سه شنبه 23 شهریور 1389, 01:02 صبح
ممنونم دوست عزیز.
کد خودمم یه کمی دستکاری کردم بالاخره Accept شد.:تشویق:

afee1990
شنبه 27 شهریور 1389, 15:29 عصر
این چرا وقتی اجرا میشه چیزی به کاربر نمایش نمیده؟

runtime.error
یک شنبه 28 شهریور 1389, 20:51 عصر
خوب چون این برنامه قراره توسط Online Judge تست بشه نه کاربر

طلیعه-988
دوشنبه 29 شهریور 1389, 00:50 صبح
این چرا وقتی اجرا میشه چیزی به کاربر نمایش نمیده؟
وقتی شما ورودی رو بدین خروجی باید چاپ بشه.
این قطعه کد خودمه که اکسپت شده:

#include <iostream>
using namespace std;
int n,m;
int cnt=0;
char field[101][101]={'0'};

void FindNo(int i,int j)
{
int J;
J=j;
for(int k=i-1;k<=i+1;k++)
{
if(k>=0)
{
for(int z=j-1;z<=j+1;z++)
{
if(field[k][z]!='*' && z>=0){
if(field[k][z]=='.')
{field[k][z]='0';}
field[k][z]++;
}
}
}
j=J;
}
}


void Print()
{
for(int i=0;i<n;i++)
{
char ch;
for(int j=0;j<m;j++)
{
ch=field[i][j];
if(ch=='.')
{cout<<'0';}
else
{cout<<ch;}
}
cout<<endl;
}
}



int main()
{
int q=0;
while(cin>>n>>m && n!=0 && m!=0)
{
q++;
if(q>1) cout<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
field[i][j]='0';
}
}
for(int i=0;i<n;i++) { cin>>field[i];}

for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(field[i][j]=='*')
{
FindNo(i,j);

}

}
}


cout<<"Field #"<<q<<":"<<endl;
Print();

}
return 0;
}