您的位置:首页 > 其它

NOIP 2017 普及组 棋盘

2018-01-02 22:15 363 查看
这篇写的很简短: http://blog.csdn.net/C20190102/article/details/78549947

还有6组数据未通过, 已经通过14组

12 40

1 1 1

1 3 0

2 6 1

2 1 0

2 4 1

2 3 0

3 8 0

3 6 1

3 3 1

4 4 0

4 1 0

4 3 1

4 8 1

5 3 1

5 4 1

5 2 0

5 9 0

5 6 1

6 5 1

6 9 0

7 9 0

7 5 1

7 1 1

7 3 1

8 8 0

8 12 0

9 7 0

9 10 1

9 3 0

9 12 1

10 6 0

10 7 1

10 12 1

10 11 1

11 5 1

11 8 0

11 10 0

12 8 1

12 10 1

12 12 0

输出 27 

未通过

#include <bits/stdc++.h>
using namespace std;
int flag=0;
int m,n;
const int MM= 2000000000;
const  int  RED = 0;
const int   YELLOW=1;
const int   WHITE = 2;
//	  0代表红色 	  1代表黄色      2代表无色      3 无色变成的红色     4 无色变成的黄色
bool visited[1001][1001];
int chess[101][101];
int dx[]={0,0, -1,1};
int dy[]={-1,1,0,0};
int dp[101][101][3][2];

bool in(int newx,int newy){
if( newx>=1 && newx<=m && newy>=1 && newy<=m ) return true;
else return false;
}

//只通过11组数据,未通过9数据,考虑搜索的时候考虑记忆化搜索
// 考虑以后, 未通过6组数据
int  search(int x,int y, int color , int magic ){

if( dp[x][y][color][magic] !=MM ) return dp[x][y][color][magic] ;

if( x==m && y==m ){
flag = 1;
return dp[x][y][color][magic] = 0;
}
int temp=MM;
for(int i=0;i<4;i++) {
int newx=x+dx[i];
int newy=y+dy[i];
if(   in(newx, newy) && visited[newx][newy] == false  )  {

if(   ( chess[newx][newy]==RED && color==RED )
|| (chess[newx][newy]==YELLOW && color==YELLOW ) ) {

visited[newx][newy]=true;
temp = min( temp,  search( newx,newy, chess[newx][newy], 0) );
visited[newx][newy]=false;
}

if(    (chess[newx][newy]==RED && (  color==YELLOW  ) )
|| (chess[newx][newy]==YELLOW && (  color==RED  ) )  ) {

visited[newx][newy]=true;
temp = min ( temp, search(newx,newy,chess[newx][newy], 0) + 1 );
visited[newx][newy]=false;
}

if(  chess[newx][newy] == WHITE  && magic == 0  ){

visited[newx][newy]=true;
temp = min( temp,  search(newx,newy, YELLOW , 1) + ( color==YELLOW? 2: 3));
visited[newx][newy]=false;

visited[newx][newy]=true;
temp = min( temp,  search(newx,newy, RED , 1) + ( color==RED? 2: 3 ));
visited[newx][newy]=false;
}
}
}

return dp[x][y][color][magic]= temp ;

}

int main(){
// freopen("chess.in","r",stdin);
// freopen("chess.out","w",stdout);
cin>>m>>n;
for(int i=
a1b9
1;i<=m;i++){
for(int j=1;j<=m;j++)  {
chess[i][j]=WHITE;
visited[i][j]=false;
}
}

for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
for(int s=0;s<3;s++)
for(int t=0;t<2;t++)
dp[i][j][s][t]=MM;

//memset( dp, 0 ,sizeof(dp));

for(int i=1;i<=n;i++){
int x,y,colour;
cin>>x>>y>>colour;
chess[x][y]=colour;
}

visited[1][1]=true;

int ans= search(1,1,chess[1][1],0);

if( flag==0 )  cout <<-1;
else cout << ans;
//else cout << dp[ 1 ][ 1 ] [ chess[1][1] ][ 0 ];

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: