看错题面,还写挂每个格子对应id的式子
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
int n,m,k,ans,tot=1,hd[(int)1e5+9],cur[(int)1e5+9],s,t,dep[(int)1e5+9];
pii dir[4]={{1,0},{0,1},{-1,0},{0,-1}};
struct Edge{
int to,nxt,v;
}e[(int)2e5+9];
void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}
bool bfs(int st,int en){
for(int i=0;i<=t;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}
int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}
int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,1<<29);
}
return ret;
}
inline int id(int x,int y){return (x-1)*m+y;}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
s=n*m+1,t=n*m+2;
for(int i=1,x;i<=n;++i)for(int j=1;j<=m;++j){
cin>>x;
if(x==1)add(s,id(i,j),1<<29);
else if(x==2)add(id(i,j),t,1<<29);
for(auto [xx,yy]:dir){
xx+=i,yy+=j;
if(xx<=n&&xx>=1&&yy<=m&&yy>=1){
add(id(i,j),id(xx,yy),1);
}
}
}
cout<<dinic(s,t);
return 0;
}