10189 - Minesweeper

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10189 - Minesweeper

Post by lighted »

Problem description says
There must be an empty line between field outputs.
Try to read this thread before posting
brianfry713 wrote:
brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Echoless
New poster
Posts: 4
Joined: Fri Dec 05, 2014 3:27 am

Re: 10189 - Minesweeper

Post by Echoless »

Code: Select all

 Removed after being accepted.
}
Last edited by Echoless on Fri Dec 05, 2014 5:50 pm, edited 1 time in total.
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10189 - Minesweeper

Post by lighted »

Don't remove it, print blank line between cases and don't print blank line after last case.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Echoless
New poster
Posts: 4
Joined: Fri Dec 05, 2014 3:27 am

Re: 10189 - Minesweeper

Post by Echoless »

Thanks a lot! It finally worked!
lazyplatoon
New poster
Posts: 4
Joined: Wed Dec 10, 2014 5:00 am

Re: 10189 - Minesweeper

Post by lazyplatoon »

I got Runtime Error... Any suggestions how to resolve it ???

Code: Select all

#include <iostream>

using namespace std;

int main(){

    int m, n, count = 0;
    while(cin >> m >> n){
        if(m == 0 && n == 0) break;

        char mine[m][n];
        int result[m][n];

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                result[i][j] = 0;
            }
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){

                cin >> mine[i][j];

                if(mine[i][j] == '*') {
                    result[i][j] = 11;

                    if(i - 1 >= 0 && j - 1 >= 0) result[i - 1][j - 1]++;
                    if(i - 1 >= 0) result[i - 1][j]++;
                    if(i - 1 >= 0 && j + 1 < n) result[i - 1][j + 1]++;
                    if(j - 1 >= 0) result[i][j - 1]++;
                    if(j + 1 < n) result[i][j + 1]++;
                    if(i + 1 < n && j - 1 >= 0) result[i + 1][j - 1]++;
                    if(i + 1 < n) result[i + 1][j]++;
                    if(i + 1 < n && j + 1 < n) result[i + 1][j + 1]++;
                }
            }
        }
        cout << "Field #" << ++count << ":"  << endl;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(result[i][j] >= 0 && result[i][j] <= 8) cout << result[i][j];
                else cout << '*';
            }
            cout << endl;
        }
        cout << endl;
    }
	return 0;
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10189 - Minesweeper

Post by lighted »

Usually when you get RE try to increase array bounds.

Code: Select all

char mine[m + 1][n + 1];
int result[m + 1][n + 1];
Don't print blank line after last line
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
lazyplatoon
New poster
Posts: 4
Joined: Wed Dec 10, 2014 5:00 am

Re: 10189 - Minesweeper

Post by lazyplatoon »

lighted wrote:Usually when you get RE try to increase array bounds.

Code: Select all

char mine[m + 1][n + 1];
int result[m + 1][n + 1];
Don't print blank line after last line
It didn't work... Now I got WA...
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10189 - Minesweeper

Post by brianfry713 »

Post your updated code.
Check input and AC output for thousands of problems on uDebug!
code.collector
New poster
Posts: 1
Joined: Wed Dec 24, 2014 5:47 pm

Re: 10189 - Minesweeper

Post by code.collector »

hi
i wrote this code but it's answer is wrong.
what's wrong with this.
thanks

Code: Select all

#include<iostream>
#include<sstream>
using namespace std;

void fillZero(char (&final)[102][102])
{
	for(int i=0;i<102;i++)
		for(int j=0;j<102;j++)
			final[i][j]='0';
}

void calculateMines(char a[102][102],char (&final)[102][102],int n,int m)
{
	int count=0;
	int i,j;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			if(a[i][j]=='*')
				final[i][j]='*';
		    if (a[i][j]=='.')
			{
				if(i>0 && j>0 && a[i-1][j-1]=='*') count++;
				if(i>0 && a[i-1][j]=='*') count++;
				if(i>0 && j<m-1 && a[i-1][j+1]=='*') count++;
				if(j>0 && a[i][j-1]=='*') count++;
				if(j<m-1 && a[i][j+1]=='*') count++;
				if(i<n-1 && j>0 && a[i+1][j-1]=='*') count++;
				if(i<n-1 && a[i+1][j]=='*') count++;
				if(i<n-1 && j<m-1 && a[i+1][j+1]=='*') count++;
			}
			if(a[i][j]=='.') final[i][j]='0'+count;
			count=0;
		}
	return;
}

int main()
{
	char array[102][102];
	char finalArray[102][102];
	int m,n,i,j;
	int caseCount=0;
	while(cin>>n>>m,m!=0,n!=0)
	{
		caseCount++;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				cin>>array[i][j];
	///////////////////////////////////////////////////
	fillZero(finalArray);
	calculateMines(array,finalArray,n,m);
	///////////////////////////////////////////////////
	cout<<"Field #"<<caseCount<<':'<<"\n";
	for(i=0;i<n;i++){
		for(j=0;j<m;j++)
			cout<<finalArray[i][j];
		cout<<"\n";
		}
	cout<<"\n";
	}
	return 0;
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10189 - Minesweeper

Post by lighted »

There must be an empty line between field outputs.
Don't print blank line after last line.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 10189 - Minesweeper

Post by Xenon Kfr »

Why Runtime error??

Code: Select all

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int m=5,n=5,i,j;
    int Z=0;
    while(cin>>m>>n && m!=0 &&n!=0)
    {
        char a[m][n];

        vector<int> star;

        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='*')
                {
                    star.push_back(i*10+j);
                }
                a[i][j]=0;
            }
        }

        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(find(star.begin(), star.end(), i*10+j)!=star.end())
                {
                    if(i==0 && j==0)
                    {
                        a[i][j+1]++;
                        a[i+1][j]++;
                        a[i+1][j+1]++;
                    }
                    else if(i==0 && j==n-1)
                    {
                        a[i][j-1]++;
                        a[i+1][j-1]++;
                        a[i+1][j]++;
                    }
                    else if(i==m-1 && j==0)
                    {
                        a[i-1][j]++;
                        a[i][j+1]++;
                        a[i-1][j+1]++;
                    }
                    else if(i==m-1 && j==n-1)
                    {
                        a[i][j-1]++;
                        a[i-1][j]++;
                        a[i-1][j-1]++;
                    }
                    else if(i==0)
                    {
                        a[i+1][j-1]++;
                        a[i+1][j]++;
                        a[i+1][j+1]++;

                        a[i][j-1]++;
                        a[i][j+1]++;
                    }
                    else if(j==0)
                    {
                        a[i][j+1]++;
                        a[i+1][j+1]++;
                        a[i-1][j+1]++;

                        a[i-1][j]++;
                        a[i+1][j]++;
                    }
                    else if(i==m-1)
                    {
                        a[i-1][j-1]++;
                        a[i-1][j]++;
                        a[i-1][j+1]++;

                        a[i][j-1]++;
                        a[i][j+1]++;
                    }
                    else if(j==n-1)
                    {
                        a[i-1][j-1]++;
                        a[i][j-1]++;
                        a[i+1][j-1]++;

                        a[i-1][j]++;
                        a[i+1][j]++;
                    }
                    else
                    {
                        a[i-1][j-1]++;
                        a[i-1][j+1]++;
                        a[i+1][j-1]++;
                        a[i+1][j+1]++;

                        a[i-1][j]++;
                        a[i+1][j]++;

                        a[i][j-1]++;
                        a[i][j+1]++;
                    }
                }
            }
        }

        cout<<"Field #"<<++Z<<":"<<endl;
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                if(find(star.begin(), star.end(), i*10+j)!=star.end())
                    cout<<"*";
                else
                    printf("%c",a[i][j]+'0');
                if((j+1)%n==0)
                    cout<<endl;
            }
        }

    }
    return 0;
}


lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10189 - Minesweeper

Post by lighted »

Code: Select all

char a[m + 1][n + 1];
Increase array bounds. Print blank line between field outputs.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
ejspeiro
New poster
Posts: 2
Joined: Sat Jan 24, 2015 9:51 am

Re: 10189 - Minesweeper

Post by ejspeiro »

Code: Select all

#include <iostream>

// With this function, we compute the number of adjacent mines, for the
// (ii,jj)-th position of the board. We assume that for every value of (ii,jj),
// we will NOT be located on a mine, since the main module would have already
// validated this.
int CountMinesAround(const int& ii,
                     const int& jj,
                     const char board[1 + 100 + 1][1 + 100 + 1]) {

  int mines {};  // Amount of mines around this location.

  mines += board[ii - 1][jj - 1] == '*'? 1: 0;
  mines += board[ii - 1][jj + 0] == '*'? 1: 0;
  mines += board[ii - 1][jj + 1] == '*'? 1: 0;
  mines += board[ii + 0][jj - 1] == '*'? 1: 0;
  mines += board[ii + 0][jj + 1] == '*'? 1: 0;
  mines += board[ii + 1][jj - 1] == '*'? 1: 0;
  mines += board[ii + 1][jj + 0] == '*'? 1: 0;
  mines += board[ii + 1][jj + 1] == '*'? 1: 0;

  return mines;
}

int main () {

  char board[1 + 100 + 1][1 + 100 + 1]; // Board for each test case.
  int nn;                               // Lines of each test case.
  int mm;                               // Columns per line of each test case.
  int test_number {};                   // Counter for the test number.

  std::cin >> nn >> mm;

  while (nn != 0 && mm != 0) {

    // Clear the array.
    for (auto ii = 0; ii < 102; ++ii) {
      for (auto jj = 0; jj < 102; ++jj) {
        board[ii][jj] = '.';
      }
    }
    // Store the complete board first.
    for (auto ii = 0; ii < nn; ++ii) {
      for (auto jj = 0; jj < mm; ++jj) {
        std::cin >> board[ii][jj];
      }
    }
    // Print the test case, and then traverse the board to count for mines.
    std::cout << "Field #" << ++test_number << ':' << std::endl;
    for (auto ii = 0; ii < nn; ++ii) {
      for (auto jj = 0; jj < mm; ++jj) {
        if (board[ii][jj] == '*') {
          std::cout << '*';
        } else {
          std::cout << CountMinesAround(ii,jj,board);
        }
      }
      std::cout << std::endl;
    }
    std::cin >> nn >> mm;
    if (nn != 0 && mm != 0) {
      std::cout << std::endl;
    }
  }
  return 0;
}
I have tried many critical inputs from many sites, and my code produces the right output but I'm still getting WA... any hints? Thanks,
ejspeiro
New poster
Posts: 2
Joined: Sat Jan 24, 2015 9:51 am

Re: 10189 - Minesweeper

Post by ejspeiro »

ejspeiro wrote:

Code: Select all

#include <iostream>

// With this function, we compute the number of adjacent mines, for the
// (ii,jj)-th position of the board. We assume that for every value of (ii,jj),
// we will NOT be located on a mine, since the main module would have already
// validated this.
int CountMinesAround(const int& ii,
                     const int& jj,
                     const char board[1 + 100 + 1][1 + 100 + 1]) {

  int mines {};  // Amount of mines around this location.

  mines += board[ii - 1][jj - 1] == '*'? 1: 0;
  mines += board[ii - 1][jj + 0] == '*'? 1: 0;
  mines += board[ii - 1][jj + 1] == '*'? 1: 0;
  mines += board[ii + 0][jj - 1] == '*'? 1: 0;
  mines += board[ii + 0][jj + 1] == '*'? 1: 0;
  mines += board[ii + 1][jj - 1] == '*'? 1: 0;
  mines += board[ii + 1][jj + 0] == '*'? 1: 0;
  mines += board[ii + 1][jj + 1] == '*'? 1: 0;

  return mines;
}

int main () {

  char board[1 + 100 + 1][1 + 100 + 1]; // Board for each test case.
  int nn;                               // Lines of each test case.
  int mm;                               // Columns per line of each test case.
  int test_number {};                   // Counter for the test number.

  std::cin >> nn >> mm;

  while (nn != 0 && mm != 0) {

    // Clear the array.
    for (auto ii = 0; ii < 102; ++ii) {
      for (auto jj = 0; jj < 102; ++jj) {
        board[ii][jj] = '.';
      }
    }
    // Store the complete board first.
    for (auto ii = 0; ii < nn; ++ii) {
      for (auto jj = 0; jj < mm; ++jj) {
        std::cin >> board[ii][jj];
      }
    }
    // Print the test case, and then traverse the board to count for mines.
    std::cout << "Field #" << ++test_number << ':' << std::endl;
    for (auto ii = 0; ii < nn; ++ii) {
      for (auto jj = 0; jj < mm; ++jj) {
        if (board[ii][jj] == '*') {
          std::cout << '*';
        } else {
          std::cout << CountMinesAround(ii,jj,board);
        }
      }
      std::cout << std::endl;
    }
    std::cin >> nn >> mm;
    if (nn != 0 && mm != 0) {
      std::cout << std::endl;
    }
  }
  return 0;
}
I have tried many critical inputs from many sites, and my code produces the right output but I'm still getting WA... any hints? Thanks,
Problem solved! Padding issues! ;)
morcef
New poster
Posts: 2
Joined: Wed Feb 11, 2015 8:27 pm

Re: 10189 - Minesweeper

Post by morcef »

Code: Select all

#include<stdio.h>
char matriz[200][200];
int resp[200][200];

void checkviz(int x, int y)
{
    int dx[] = { 1, 1, 1, 0, 0, -1, -1, -1 };
    int dy[] = { 1, -1, 0, 1, -1, 1, 0, -1 };
    int i,j,c;

    if(matriz[x][y] == '*')
            resp[x][y] = -1;

    else if(y == 0)
    {
            c = 0;
            if(matriz[x-1][y] == '*')
                c++;
            if(matriz[x-1][y+1] == '*')
                c++;
            if(matriz[x][y+1] == '*')
                c++;
            if(matriz[x+1][y+1] == '*')
                c++;
            if(matriz[x+1][y] == '*')
                c++;
            resp[x][y] = c;
    }
    else
    {
        c = 0;
        for(i=0; i<8; i++)
            if(matriz[x+dx[i]][y+dy[i]] == '*')
                    c++;
        resp[x][y] = c;
    }

}
int main()
{
    int n,m,i,j,t=1;
    while(scanf("%d %d",&n,&m) && (n || m))
    {
        for(i=0; i<=n+1; i++)
            resp[i][m+1] = 0;
        for(j=0; j<=m+1; j++)
        {
            resp[0][j] = 0;
            resp[n+1][j] = 0;
        }
        for(i=1; i<=n; i++)
            scanf("%s",matriz[i]);
        for(i=1; i<=n; i++)
            for(j=0; j<m; j++)
                checkviz(i,j);
        printf("Field #%d:\n",t++);
        for(i=1; i<=n; i++)
        {
            for(j=0; j<m; j++)
                if(resp[i][j] == -1)
                    printf("*");
                else
                    printf("%d",resp[i][j]);
            printf("\n");
        }
        printf("\n");

    }

    return 0;
}
somebody can explain me, why this code returns Wrong Answer? :-?
I tried follow the hints in this post but i can't found the error
Post Reply

Return to “Volume 101 (10100-10199)”