7.18
二维数组中有关字符的问题
014
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[105][105];
bool b[105][105];
int n, m;
int dx[9] = {0, 1, -1, 0, 0, 1, 1, -1, -1};
int dy[9] = {0, 0, 0, 1, -1, 1, -1, 1, -1};
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
cin >> a[i][j];
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= m; j ++ )
{
if (a[i][j] == '*') cout << a[i][j];
else
{
int sum = 0;
for (int k = 1; k <= 8; k ++ )
{
int x = dx[k] + i;
int y = dy[k] + j;
if (a[x][y] == '*') sum ++ ;
}
cout << sum;
}
}
cout << endl;
}
return 0;
}
016
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[105][105], b[105][105];
int n, m, t;
int dx[5] = {0, 1, -1, 0, 0};
int dy[5] = {0, 0, 0, 1, -1};
void bs()
{
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
if (a[i][j] == 'R')
{
for (int k = 1; k <= 4; k ++ )
{
int x = dx[k] + i, y = dy[k] + j;
if (a[x][y] == 'S') b[x][y] = 'R';
}
}
else if (a[i][j] == 'S')
{
for (int k = 1; k <= 4; k ++ )
{
int x = dx[k] + i, y = dy[k] + j;
if (a[x][y] == 'P') b[x][y] = 'S';
}
}
else
{
for (int k = 1; k <= 4; k ++ )
{
int x = dx[k] + i, y = dy[k] + j;
if (a[x][y] == 'R') b[x][y] = 'P';
}
}
}
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
a[i][j] = b[i][j];
}
int main()
{
cin >> n >> m >> t;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
cin >> a[i][j];
b[i][j] = a[i][j];
}
while (t -- )
{
bs();
}
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= m; j ++ )
cout << a[i][j];
cout << endl;
}
return 0;
}
017
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[105][105];
bool b[105][105];
int n, m, sum;
int dx[5] = {0, 1, -1, 0, 0};
int dy[5] = {0, 0, 0, 1, -1};
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
cin >> a[i][j];
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
if (a[i][j] == '#' && b[i][j] == 0)
{
b[i][j] = 1;
for (int k = 1; k <= 4; k ++ )
{
int x = dx[k] + i;
int y = dy[k] + j;
if (a[x][y] == '#' && b[x][y] == 0)
{
b[x][y] = 1;
break;
}
}
sum ++ ;
}
}
cout << sum << endl;
return 0;
}
020
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
char a[105][105];
int n, m;
int main()
{
string s;
cin >> m >> s;
int len = s.size();
int i = 1, j = 1;
n = len / m;
for (int k = 0; k < len; k ++ )
{
a[i][j] = s[k];
if (i % 2 == 1)
{
if (j == m) i ++ ;
else j ++;
}
else
{
if (j == 1) i ++ ;
else j --;
}
}
for (j = 1; j <= m; j ++ )
for (i = 1; i <= n; i ++ )
cout << a[i][j];
return 0;
}