Codeforces 1065D Three Pieces

本文探讨了一个n*n矩阵中,采用不同走法(直线、斜线、日字)从数字1位置出发,按顺序访问至n^2位置的路径优化问题。通过BFS搜索策略,设定状态转移方程,实现精力值最小化的同时,减少走法切换次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

有一个n * n 的矩阵,不重不漏地随机放着 1到 n*n 的每个数。现在你站在数字1的位置,每次可以有三种走法:

  • 走直线,四个方向。一次可以走多个单位,不仅只能走到相邻的位置,就像中国象棋里的车。
  • 走斜线,四个方向。一次可以走多个单位,像国际象棋里的象。
  • 走日字,八个方向,一次只能走一个单位。就像中国象棋里的马。
    每走一次,需要花费一个精力值。如果切换了一次走法,需要额外花费一个精力值。而且要求,必须先访问1,再访问2,然后是3,以此类推,一直到n^2,但是,在从 i 走到 i+1的过程中,可以经过其它的点,比如在中途某个点停一下,换了走法,继续走。

问最少花费多少精力值可以走完n^2个点。精力最小的时候要求切换走法次数也最少。
输出最小精力和最少切换走法次数。

分析:

bfs搜索,状态可以这么设定dp[i][j][z][t][k]表示到达g[i][j]这个点,用的是第z种走法,已经换了t次走法,已经按顺序访问了k个点,此时的最小精力值。
转移分为两种:

  • 不换方式,按照当前方式继续走下去。
  • 原地换一下方式,换完不动。

代码:

#include <bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
#define ll long long
#define PII pair<int,int>
#define sz(x) (int)(x.size())
using namespace std;
const int INF = 0x3f3f3f3f;
struct node{
    int x,y,z,t,k;
    node(int _x,int _y,int _z,int _t,int _k) {
        x = _x;
        y = _y;
        z = _z; //当前方式
        t = _t; //换了多少次
        k = _k; //经过前k个点
    }
};
int n;
int g[15][15];
int dp[11][11][3][201][101]; //0是日,1是直,2是斜线
int dx1[8][2] = {{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}}; //日
int dx2[4][2] = {{-1,0},{1,0},{0,1},{0,-1}}; //直线
int dx3[4][2] = {{-1,-1},{-1,1},{1,-1},{1,1}}; //斜线
int sx,sy,tx,ty;
void bfs(int sx,int sy){
    memset(dp,-1,sizeof dp);
    dp[sx][sy][0][0][1] = dp[sx][sy][1][0][1] = dp[sx][sy][2][0][1] = 0;
    queue<node> q;
    q.push(node(sx,sy,0,0,1));
    q.push(node(sx,sy,1,0,1));
    q.push(node(sx,sy,2,0,1));

    while(!q.empty()){
        node nd = q.front();q.pop();
        int x = nd.x, y = nd.y, z = nd.z, t = nd.t , k = nd.k;
        //原地换走法
        for(int i=0;i<3;i++){
        	if(i==z)	continue;
        	if(dp[x][y][i][t+1][k]!=-1) continue;
        	dp[x][y][i][t+1][k] = dp[x][y][z][t][k]+1;
        	q.push(node(x,y,i,t+1,k));

        }
        //不换状态继续走
        if(z==0) {
            for(int i=0;i<8;i++){
                int xx = x+dx1[i][0], yy = y + dx1[i][1],kk = k;
                if(xx<1 || xx>n || yy<1 || yy>n) continue;
                if(g[xx][yy] == k+1) kk++;
                if(dp[xx][yy][z][t][kk]!=-1) continue;
                dp[xx][yy][z][t][kk] = dp[x][y][z][t][k]+1;
                q.push(node(xx,yy,z,t,kk));
            }
        }
        if(z==1) {
            for(int j=1;j<=10;j++){
                for(int i=0;i<4;i++){
	                int xx = x+j*dx2[i][0], yy = y + j*dx2[i][1],kk = k;
	                if(xx<1 || xx>n || yy<1 || yy>n) continue;
	                if(g[xx][yy] == k+1) kk++;
	                if(dp[xx][yy][z][t][kk]!=-1) continue;
	                dp[xx][yy][z][t][kk] = dp[x][y][z][t][k]+1;
	                q.push(node(xx,yy,z,t,kk));
            	}
            }
            
        }
        if(z==2) {
            for(int j=1;j<=10;j++){
                for(int i=0;i<4;i++){
	                int xx = x+j*dx3[i][0], yy = y + j*dx3[i][1],kk = k;
	                if(xx<1 || xx>n || yy<1 || yy>n) continue;
	                if(g[xx][yy] == k+1) kk++;
	                if(dp[xx][yy][z][t][kk]!=-1) continue;
	                dp[xx][yy][z][t][kk] = dp[x][y][z][t][k]+1;
	                q.push(node(xx,yy,z,t,kk));
            	}
            }
        }
    }
}
int main(){
    cin>>n;
    
    for(int i=1;i<=n;i++){
	    for(int j=1;j<=n;j++) {
	        cin>>g[i][j];
	        if(g[i][j]==1) {
	            sx = i;
	            sy = j;
	        }
	        if(g[i][j] == n*n){
	        	tx = i;
	        	ty = j;
	        }
	    }
    }
    bfs(sx,sy);
    int ans = INF;
    for(int z=0;z<3;z++){ //当前方式
    	for(int t=0;t<201;t++){ //换了多少次
    		if(dp[tx][ty][z][t][n*n]!=-1){
    			ans = min(dp[tx][ty][z][t][n*n],ans);
    		}
    	}
    }
    int ok=0;
    for(int t=0;t<201;t++){
    	for(int z=0;z<3;z++){
    		if(dp[tx][ty][z][t][n*n] == ans){
    			cout << ans << " " << t << endl;
    			ok = 1;	break;
    		}
    	}
    	if(ok)	break;
    }
    return 0;
}
### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值