谁能帮我改一下错
查看原帖
谁能帮我改一下错
1098953
M_Jun楼主2024/7/8 19:13
#include<bits/stdc++.h>
using namespace std ;

const int ief = 0x3f3f3f , N = 1e3 + 15 ;

int dis[N][N] ;
bool mark[N][N] ;
int a[N][N] ;
int n , m ;

const int dx[4] = {0, 0, -1, 1} ;
const int dy[4] = {-1, 1, 0, 0} ;

void dfs (int x, int y, int cost, int color){
	if(cost > dis[m][m]) return ;
	if(x == n and y == n) {
		dis[m][m] = min(dis[m][m], cost) ;
		return ;
	}
	for(int i = 0 ; i < 4 ; i ++) {
		int nx = x + dx[i] ;
		int ny = y + dy[i] ;
		if(nx < 1 || nx > m || ny < 1 || ny > m) continue ;
		if(a[x][y] || a[nx][ny]) {
			if(!a[nx][ny]) {
				if(cost + 2 >= dis[nx][ny]) continue ;
				mark[nx][ny] = 1 ;
				dis[nx][ny] = cost + 2 ;
				dfs(nx, ny, cost + 2, color) ;
				mark[nx][ny] = 0 ;
			}else {
				if(a[nx][ny] == color and cost < dis[nx][ny]) {
					dis[nx][ny] = cost ;
					mark[nx][ny] = 1 ;
					dfs(nx, ny, cost, color) ;
					mark[nx][ny] = 0 ;
				}else if(cost + 1 < dis[nx][ny]) {
					dis[nx][ny] = cost + 1 ;
					mark[nx][ny] = 1 ;
					dfs(nx, ny, cost + 1, color) ;
					mark[nx][ny] = 0 ;
				}
			}
		}
	}
}

int main ( ) {
	memset(dis, ief, sizeof dis ) ;
	cin >> m >> n ;
	for(int i = 1 ; i <= n ; i ++){
		int x, y, c ;
		cin >> x >> y >> c ;
		a[x][y] = c++ ;
	}
	
	dfs(1, 1, 0, a[1][1]) ;
	
	if(dis[m][m] == ief) {
		cout << "-1" << endl ;
	}else{
		cout << dis[m][m] << endl ;
	}
	
	return 0 ;
} 

请问哪里有问题全WA了

2024/7/8 19:13
加载中...