Re: 10189 Minesweeper WA....
Posted: Mon Mar 10, 2014 8:15 pm
Code: Select all
import java.io.*;
import java.util.*;
class Main
{
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin()
{
String input; //entra com uma entrada
StringTokenizer idata; //quebra essa entrada em pedaƧoes
int a, b;
int count = 0;
while ((input = Main.ReadLn (255)) != null) //A string input recebe a linha inteira
{
if(input.equals("0 0")) break; //eh string, logo, usa equals
idata = new StringTokenizer (input);
a = Integer.parseInt (idata.nextToken());
b = Integer.parseInt (idata.nextToken());
char[][] array = new char[a][b];
for(int i = 0;i<array.length;i++){
input = Main.ReadLn(255); //le a string a ser inserida pra formar o campo
for(int j = 0; j<array[i].length; j++){
array[i][j] = input.charAt(j);
}
}
count++;
printGame(ProduceGame(array, a, b),count);
System.out.println();
}
}
char[][] ProduceGame(char[][] array, int a, int b){
char[][] matrix = new char[a][b];
matrix = inicialize(a,b);
String x = "*";
int temp = 0;
for(int i = 0; i< a; i++){
for(int j = 0; j< b; j ++){
if(array[i][j] == x.charAt(0)){
matrix[i][j] = array[i][j];
//48 eh zero na tabela ASCII, entao so adicionar 48 a tudo
if(i!=0 && j!=0) {
temp = Character.getNumericValue(matrix[i-1][j-1]);
temp++;
if(array[i-1][j-1] != x.charAt(0)) matrix[i-1][j-1] = (char)(temp+48); //verifica se a posicao n contem bomba
}
if(i!=0){
temp = Character.getNumericValue(matrix[i-1][j]);
temp++;
if(array[i-1][j] != x.charAt(0)) matrix[i-1][j] = (char)(temp+48);
}
if(j!=0){
temp = Character.getNumericValue(matrix[i][j-1]);
temp++;
if(array[i][j-1] != x.charAt(0)) matrix[i][j-1]= (char)(temp+48);
}
if(i != a-1 && j !=0){
temp = Character.getNumericValue(matrix[i+1][j-1]);
temp++;
if(array[i+1][j-1] != x.charAt(0)) matrix[i+1][j-1] = (char)(temp+48);
}
if(i != 0 && j != b-1){
temp = Character.getNumericValue(matrix[i-1][j+1]);
temp++;
if(array[i-1][j+1] != x.charAt(0)) matrix[i-1][j+1] = (char)(temp+48);
}
if(i!=a-1 && j!= b-1){
temp = Character.getNumericValue(matrix[i+1][j+1]);
temp++;
if(array[i+1][j+1] != x.charAt(0)) matrix[i+1][j+1] = (char)(temp+48);
}
if(i!=a-1){
temp = Character.getNumericValue(matrix[i+1][j]);
temp++;
if(array[i+1][j] != x.charAt(0)) matrix[i+1][j] = (char)(temp+48);
}
if(j!=b-1){
temp = Character.getNumericValue(matrix[i][j+1]);
temp++;
if(array[i][j+1] != x.charAt(0)) matrix[i][j+1] = (char)(temp+48);
}
}
temp = 0;
}
}
return matrix;
}
char[][] inicialize(int a, int b){
char[][] produced = new char[a][b];
for(int i = 0; i< a; i++){
for(int j = 0; j< b; j++){
produced[i][j] = '0';
}
}
return produced;
}
void printGame(char[][] a, int count){
System.out.println("Field #"+count+":");
for(int i = 0; i<a.length; i++){
for(int j = 0; j< a[i].length; j++){
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
Code: Select all
#include <iostream>
#include <algorithm>
using namespace std;
int n=1,m=1;
char a[101][101], b[101][101];
char calc(int p, int q){
int c=0;
if(a[p][q]=='*') return '*';
for(int i=p-1 ; i<=p+1 ; i++){
for(int j=q-1 ; j<=q+1 ; j++){
if( !((i==p && j==q) && (i<0 && i>=n) && (j<0 && j>=m)) )
if(a[i][j]=='*') c++;
}
}
return c+'0';
}
int main(){
int tc=0;
while(true){
cin >> n >> m;
if(n==0 && m==0) break;
for(int i=0 ; i<n; i++){
for(int j=0 ; j<m; j++){
cin >> a[i][j];
}
}
for(int i=0 ; i<n; i++){
for(int j=0 ; j<m; j++){
b[i][j]=calc(i,j);
}
}
cout << "Field #" << ++tc << ":" << endl;
for(int i=0 ; i<n; i++){
for(int j=0 ; j<m; j++){
cout << b[i][j];
}
cout << endl;
}
cout << endl;
}
}
I suggest you always start by taking the time to read the previous messages in the forums. Check for example the message posted by brianfry713 around Nov 14, 2013, which is relevant to your program.double_zero wrote:Somebody Please Help Me, I Don't Know Why I Get Wrong Answer
Code: Select all
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
int m,n,field=0;
while(cin>>m>>n,m || n)
{
char a[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]='0';
cin.ignore();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
char ch=getchar();
if(ch=='*')
{
a[i-1][j-1]+=(i>0&&j>0&&a[i-1][j-1]!='*');
a[i-1][j]+=(i>0&&a[i-1][j]!='*');
a[i-1][j+1]+=(i>0&&j<n&&a[i-1][j+1]!='*');
a[i][j-1]+=(j>0&&a[i][j-1]!='*');
a[i][j]='*';
a[i][j+1]+=(j<n&&a[i][j+1]!='*');
a[i+1][j-1]+=(i<m&&j>0&&a[i+1][j-1]!='*');
a[i+1][j]+=(i<m&&a[i+1][j]!='*');
a[i+1][j+1]+=(i<m&&j<n&&a[i+1][j+1]!='*');
}
}
cin.ignore();
}
if(field)cout<<endl;
cout<<"Field #"<<++field<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<a[i][j];
cout<<endl;
}
}
return 0;
}
Code: Select all
removed after ac
Code: Select all
Removed after accepted ! :D