E . Screamers in the Storm [ 问题 7824 ] [ 讨论 ]
Description
As you might remember from your first years in school, the human race invented beer brewing at least about 7000 years ago. The total amount of beer consumed from those times must be monumental and surely the rate of consumption is not going to shrink in the coming millennia.
To celebrate these facts, we invite you to implement a game, seemingly unrelated to beer brewing. It is quite possible, however, that after a successful implementation you might feel a little dizzy, just as if you have had a little bit more than your daily dose of beer…
The game is played on a rectangular M×N grid consisting of square tiles of different types.
Animals and food
There is some number of animals on the grid, each animal occupies exactly one tile.
Grass grows, sheep eat grass, wolves eat sheep, and both wolves and sheep can die of starvation.
Turns and animal actions
Each turn consists of actions in the following order:
All animals on the grid move. Each wolf moves to a neighbouring tile in the east (to the right). If the wolf’s move is not possible, the wolf moves to the westernmost tile in its row. Each sheep moves to a neighbouring tile in the south (down). If the sheep’s move is not possible, the sheep moves to the northernmost tile in its column.
If a wolf and a sheep occupy the same tile, the wolf eats the sheep and the tile is changed to a Soil with carcass tile.
If a sheep is located on a Soil with grass tile, the sheep eats the grass and the tile is changed to a Soil tile.
If a wolf hasn’t eaten in any of the last 10 turns including the current turn, it dies and the tile is changed to a Soil with carcass tile.
If a sheep didn’t eat in any of the last 5 turns including the current turn, it dies and the tile is changed to a Soil with carcass tile.
Types of tiles and their changes
There are three types of tiles. The type of a tile may be changed in the course of the game.
Soil tile: After 3 turns after the beginning of the game or after 3 turns after the tile became a Soil tile, the tile becomes a Soil with grass tile.
Soil with grass tile: If the grass is eaten by a sheep, the tile immediately becomes a Soil tile. The grass will grow again at the tile after 3 turns.
Soil with carcass tile: Whenever an animal dies on a tile of any type, the tile immediately becomes a Soil with carcass tile. Animals can still move to this tile, but the grass will never grow on this tile again. As the game progresses, more carcasses may accumulate on the tile.
Input
First line of the input contains three integers T,N and M (1≤T≤100, 1≤M,N≤20), where T is the number of turns, M is the number of rows and N is the number of columns of the grid. The following M lines contain N characters each. Characters denote the types of tiles:
. (dot character) denotes a Soil tile
S denotes a Soil tile with a sheep on it
W denotes a Soil tile with a wolf on it
Output
Output M lines each containing N characters describing the state of the grid after the end of the T-th turn. If there is an animal on a tile, output:
W for a tile with a wolf on it
S for a tile with a sheep on it
Otherwise, output:
- for a Soil with carcass tile
for a Soil with grass tile
. (dot character) for a Soil tile
Samples
Input 复制
6 6 5
…S…
…
.S…
…
…W
.S…
Output
##S##
#.###
W*.##
#S.##
Input 复制
14 3 3
S…
W…
…
Output
.##
#*#
S##
Input 复制
2 3 1
S
.
.
Output
.
.
S
Input 复制
3 3 1
S
.
.
Output
S
Input 复制
4 3 1
S
.
.
Output
S
Input 复制
5 3 1
S
.
.
Output
.
S
Source
CTU Open Contest 2019
大模拟;
原代码(错的)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#pragma GCC optiize(2)
#define maxn 0x3f3f3f3f
int t,n,m;
struct node
{
char c;
int num,f=0,ns,nw;
} s[1001][1001];
struct node2
{
int x,y,f,step;
} w[1001],sh[10001];
int t1=0,t2=0;
int main()
{
cin>>t>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
cin>>s[i][j].c;
s[i][j].ns=0;
s[i][j].nw=0;
if(s[i][j].c=='W')
{
w[++t1].x=i;
w[t1].y=j;
w[t1].f=1;
w[t1].step=0;
s[i][j].nw=1;
}
else if(s[i][j].c=='S')
{
sh[++t2].x=i;
sh[t2].y=j;
sh[t2].f=1;
sh[t2].step=0;
s[i][j].ns=1;
}
s[i][j].c='.';
s[i][j].num=0;
}
}
while(t--)
{
for(int i=1; i<=t2; i++)
{
if(sh[i].f==1)
{
sh[i].x++;
if(sh[i].x>n)
{
sh[i].x=1;
s[n][sh[i].y].ns=0;
s[sh[i].x][sh[i].y].ns=i;
}
else
{
s[sh[i].x-1][sh[i].y].ns=0;
s[sh[i].x][sh[i].y].ns=i;
}
}
}
for(int i=1; i<=t1; i++)
{
if(w[i].f==1)
{
w[i].y++;
if(w[i].y>m)
{
w[i].y=1;
s[w[i].x][1].nw=i;
s[w[i].x][m].nw=0;
}
else
{
s[w[i].x][w[i].y].nw=i;
s[w[i].x][w[i].y-1].nw=0;
}
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(s[i][j].c=='.')
{
s[i][j].num++;
if(s[i][j].num==3)
{
s[i][j].c='#';
}
}
else if(s[i][j].c=='#')
{
s[i][j].num=0;
}
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(s[i][j].c=='.')
{
if(s[i][j].ns!=0&&s[i][j].nw==0)
{
sh[s[i][j].ns].step++;
if(sh[s[i][j].ns].step==5)
{
sh[s[i][j].ns].f=0;
s[i][j].c='*';
s[i][j].ns=0;
}
}
else if(s[i][j].ns==0&&s[i][j].nw!=0)
{
w[s[i][j].nw].step++;
if(w[s[i][j].nw].step==10)
{
w[s[i][j].nw].f=0;
s[i][j].c='*';
s[i][j].nw=0;
}
}
else if(s[i][j].ns!=0&&s[i][j].nw!=0)
{
sh[s[i][j].ns].f=0;
s[i][j].ns=0;
w[s[i][j].nw].step=0;
s[i][j].c='*';
}
}
else if(s[i][j].c=='#')
{
if(s[i][j].ns!=0&&s[i][j].nw==0)
{
if(s[i][j].num==3)
{
sh[s[i][j].ns].step++;
if(sh[s[i][j].ns].step==5)
{
sh[s[i][j].ns].step=0;
s[i][j].c='.';
s[i][j].num=0;
}
}
else if(s[i][j].num==0)
{
sh[s[i][j].ns].step=0;
s[i][j].c='.';
s[i][j].num=0;
}
}
else if(s[i][j].ns==0&&s[i][j].nw!=0)
{
w[s[i][j].nw].step++;
if(w[s[i][j].nw].step==10)
{
w[s[i][j].nw].f=0;
s[i][j].nw=0;
s[i][j].c='*';
}
}
else if(s[i][j].ns!=0&&s[i][j].nw!=0)
{
sh[s[i][j].ns].f=0;
s[i][j].ns=0;
w[s[i][j].nw].step=0;
s[i][j].c='*';
}
}
else if(s[i][j].c=='*')
{
if(s[i][j].ns!=0&&s[i][j].nw==0)
{
sh[s[i][j].ns].step++;
if(sh[s[i][j].ns].step==5)
{
s[i][j].ns=0;
sh[s[i][j].ns].f=0;
}
}
else if(s[i][j].ns==0&&s[i][j].nw!=0)
{
w[s[i][j].nw].step++;
if(w[s[i][j].nw].step==10)
{
s[i][j].nw=0;
w[s[i][j].nw].f=0;
}
}
else if(s[i][j].ns!=0&&s[i][j].nw!=0)
{
sh[s[i][j].ns].f=0;
s[i][j].ns=0;
w[s[i][j].nw].step=0;
}
}
}
}
}
for(int i=1;i<=t1;i++)
if(w[i].f==1)
s[w[i].x][w[i].y].c='W';
for(int i=1;i<=t2;i++)
if(sh[i].f==1)
s[sh[i].x][sh[i].y].c='S';
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<s[i][j].c<<' ';
}
cout<<endl;
}
return 0;
}
各位大神网友,可以帮忙看看哪错了。
看着大神的代码重写的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#pragma GCC optiize(2)
#define maxn 0x3f3f3f3f
ll t,n,m;
struct node
{
char c;
ll num,f=0,ns,nw;
} s[1001][1001];
struct node2
{
ll x,y,f,step;
} w[1001],sh[10001];
ll t1,t2;
int main()
{
cin>>t>>n>>m;
for(ll i=1; i<=n; i++)
{
for(ll j=1; j<=m; j++)
{
cin>>s[i][j].c;
if(s[i][j].c=='W')
{
w[++t1].x=i;
w[t1].y=j;
}
else if(s[i][j].c=='S')
{
sh[++t2].x=i;
sh[t2].y=j;
}
s[i][j].c='.';
}
}
/*for(ll i=1; i<=t1; i++)
cout<<w[i].x<<' '<<w[i].y<<endl;
for(ll i=1; i<=t2; i++)
cout<<sh[i].x<<' '<<sh[i].y<<endl;*/
while(t--)
{
for(ll i=1; i<=t1; i++)
if(w[i].f==0)
{
if(w[i].y+1>m)
w[i].y=1;
else
w[i].y++;
}
for(ll i=1; i<=t2; i++)
if(sh[i].f==0)
{
if(sh[i].x+1>n)
sh[i].x=1;
else
sh[i].x++;
}
for(ll i=1; i<=t1; i++)
{
ll f=0;
if(w[i].f==0)
{
for(ll j=1; j<=t2; j++)
{
if(sh[j].f==0&&w[i].x==sh[j].x&&w[i].y==sh[j].y)
{
sh[j].f++;
w[i].step=0;
s[w[i].x][w[i].y].c='*';
f++;
}
}
if(f==0)
w[i].step++;
if(w[i].step>=10)
{
w[i].f++;
s[w[i].x][w[i].y].c='*';
}
}
}
for(ll i=1; i<=t2; i++)
{
if(sh[i].f==0)
{
if(s[sh[i].x][sh[i].y].c=='#')
{
sh[i].step=0;
s[sh[i].x][sh[i].y].c='.';
s[sh[i].x][sh[i].y].num=-1;
}
else
{
sh[i].step++;
if(sh[i].step>=5)
sh[i].f++,s[sh[i].x][sh[i].y].c='*';
}
}
}
for(ll i=1; i<=n; i++)
{
for(ll j=1; j<=m; j++)
{
if(s[i][j].c=='.')
{
s[i][j].num++;
if(s[i][j].num==3)
{
s[i][j].c='#';
}
}
}
}
}
for(ll i=1; i<=t1; i++)
if(w[i].f==0)
s[w[i].x][w[i].y].c='W';
for(ll i=1; i<=t2; i++)
if(sh[i].f==0)
s[sh[i].x][sh[i].y].c='S';
for(ll i=1; i<=n; i++)
{
for(ll j=1; j<=m; j++)
{
cout<<s[i][j].c;
}
cout<<endl;
}
return 0;
}
基本全改了。。。。。。