๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/BOJ

[C++/BOJ] 11660 : ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5 (DP)

728x90

 

https://www.acmicpc.net/problem/11660

 

 

๋ฐฑ์ค€ ์‹ค๋ฒ„1

๊ทธ๋ƒฅ ํ’€์—ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚˜๊ธธ๋ž˜ ๋ญ์ง€ ์‹ถ์—ˆ๋Š”๋ฐ.. dp ๋ฌธ์ œ์˜€๋‹ค ใ…œใ…œ

index 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉด ์ด๊ฒƒ์ €๊ฒƒ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์— index 1๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ํŽธํ•˜๋‹ค

 

 

๋‚˜์˜ ํ’€์ด

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	int m;
	int num;
	int dp[1025][1025] = {0,};
	int x1;
	int x2;
	int y1;
	int y2;

	cin >> n >> m;
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= n; ++j)
		{
			cin >> num;
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + num - dp[i - 1][j - 1];
		}
	}

	for (int i = 0; i < m; ++i){
		int answer;
		cin >> x1 >> y1 >> x2 >> y2;
		// ๊ทธ๋ƒฅ ๋”ํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ -> dp ์ด์šฉ
		answer = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1 - 1][y1 - 1];
		cout << answer << "\n";
	}
	
	return 0;
}
728x90