NOIP 2017 普及组 棋盘

这篇写的很简短: 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=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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值