L1-087 机工士姆斯塔迪奥

题目 L1-087 机工士姆斯塔迪奥

image-e9044ed6

思路分析

用数组会爆空间 段错误

用unordered_map<int,unordered_map<int,bool>> 动态分配

代码实现

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

using ll = long long;

using ull = unsigned long long;

using PII = pair<int,int>;

using Pll = pair<ll,ll>;

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

const int inf = 0x3f3f3f3f;

const int N=3e4+10;

bool g[N][N];

int main() {

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int n,m,q;cin>>n>>m>>q;

	while(q--){

		int t,c;cin>>t>>c;

		c--;

		if(t==0){//第c行

			for(int j=0;j<m;j++)

				g[c][j]=true;

		}else if(t==1){//第c列

			for(int i=0;i<n;i++)

				g[i][c]=true;

		}

	}

	int cnt=0;

	for(int i=0;i<n;i++){

		for(int j=0;j<m;j++){

			if(g[i][j]==false){

				cnt++;

			}

		}

	}

	cout<<cnt;

	return 0;

}
#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

using ll = long long;

using ull = unsigned long long;

using PII = pair<int,int>;

using Pll = pair<ll,ll>;

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

const int inf = 0x3f3f3f3f;

int main() {

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int n,m,q;cin>>n>>m>>q;

	unordered_map<int, unordered_map<int, bool>> g;

	while(q--){

		int t,c;cin>>t>>c;

		c--;

		if(t==0){//第c行

			for(int j=0;j<m;j++)

				g[c][j]=true;

		}else if(t==1){//第c列

			for(int i=0;i<n;i++)

				g[i][c]=true;

		}

	}

	int cnt=0;

	for(int i=0;i<n;i++){

		for(int j=0;j<m;j++){

			if(g[i][j]==false){

				cnt++;

			}

		}

	}

	cout<<cnt;

	return 0;

}

同类题型

视频讲解


⬅️ L1-086 斯德哥尔摩火车上的题 🏠 00-天梯赛 ➡️ L1-088 静静的推荐