level 1
Chenlele3408
楼主
题目是acm的POJ2339
代码如下:
#define size 100
int a[4][2]={
{-1,0},
{1,0},
{0,-1},
{0,1}
};
char str [size][size];
char str1[size][size];
int num[size][size];
int main()
{
int i,j,r,c,t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&r,&c,&n);
for(i=0;i<r;i++)
{
scanf("%s",str[i]);
strcpy(str1[i],str[i]);
}
while(n--)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(str[i][j]=='R')
{
if( i-1>=0&&str[i-1][j]=='P') str1[i][j]='P';
if(str[i+1][j]=='P'&&i+1<r) str1[i][j]='P';
if(str[i][j-1]=='P'&&j-1>=0) str1[i][j]='P';
if(str[i][j+1]=='P'&&j+1<c) str1[i][j]='P';
}
if(str[i][j]=='S')
{
if( i-1>=0&&str[i-1][j]=='R') str1[i][j]='R';
if(str[i+1][j]=='R'&&i+1<r) str1[i][j]='R';
if(str[i][j-1]=='R'&&j-1>=0) str1[i][j]='R';
if(str[i][j+1]=='R'&&j+1<c) str1[i][j]='R';
}
if(str[i][j]=='P')
{
if( i-1>=0&&str[i-1][j]=='S') str1[i][j]='S';
if(str[i+1][j]=='S'&&i+1<r) str1[i][j]='S';
if(str[i][j-1]=='S'&&j-1>=0) str1[i][j]='S';
if(str[i][j+1]=='S'&&j+1<c) str1[i][j]='S';
}
}
strcpy(str[i],str1[i]);
}
}
for(i=0;i<r;i++)
{
printf("%s\n",str[i]);
}
}
return 0;
}
2016年01月08日 05点01分
1
代码如下:
#define size 100
int a[4][2]={
{-1,0},
{1,0},
{0,-1},
{0,1}
};
char str [size][size];
char str1[size][size];
int num[size][size];
int main()
{
int i,j,r,c,t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&r,&c,&n);
for(i=0;i<r;i++)
{
scanf("%s",str[i]);
strcpy(str1[i],str[i]);
}
while(n--)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(str[i][j]=='R')
{
if( i-1>=0&&str[i-1][j]=='P') str1[i][j]='P';
if(str[i+1][j]=='P'&&i+1<r) str1[i][j]='P';
if(str[i][j-1]=='P'&&j-1>=0) str1[i][j]='P';
if(str[i][j+1]=='P'&&j+1<c) str1[i][j]='P';
}
if(str[i][j]=='S')
{
if( i-1>=0&&str[i-1][j]=='R') str1[i][j]='R';
if(str[i+1][j]=='R'&&i+1<r) str1[i][j]='R';
if(str[i][j-1]=='R'&&j-1>=0) str1[i][j]='R';
if(str[i][j+1]=='R'&&j+1<c) str1[i][j]='R';
}
if(str[i][j]=='P')
{
if( i-1>=0&&str[i-1][j]=='S') str1[i][j]='S';
if(str[i+1][j]=='S'&&i+1<r) str1[i][j]='S';
if(str[i][j-1]=='S'&&j-1>=0) str1[i][j]='S';
if(str[i][j+1]=='S'&&j+1<c) str1[i][j]='S';
}
}
strcpy(str[i],str1[i]);
}
}
for(i=0;i<r;i++)
{
printf("%s\n",str[i]);
}
}
return 0;
}
