Try to read this thread before postingThere must be an empty line between field outputs.
brianfry713 wrote:brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
Moderator: Board moderators
Try to read this thread before postingThere must be an empty line between field outputs.
brianfry713 wrote:brianfry713 wrote:There must be an empty line between field outputs. Don't print an extra blank line at the end.
Code: Select all
Removed after being accepted.
}
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;
}
Code: Select all
char mine[m + 1][n + 1];
int result[m + 1][n + 1];
It didn't work... Now I got WA...lighted wrote:Usually when you get RE try to increase array bounds.Don't print blank line after last lineCode: Select all
char mine[m + 1][n + 1]; int result[m + 1][n + 1];
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;
}
Don't print blank line after last line.There must be an empty line between field outputs.
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;
}
Code: Select all
char a[m + 1][n + 1];
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;
}
Problem solved! Padding issues!ejspeiro wrote: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,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; }
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;
}