알고리즘/메모
행렬의 표현
sun__
2019. 9. 27. 14:23
클래스나 구조체를 사용하는 법도 있겠지만 길이가 유동적인 정사각행렬에 대해서는 이 코드가 유용할 듯 하다.
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> matrix;
matrix operator * (const matrix& a, const matrix& b){
int n = a.size();
matrix c(n, vector<ll>(n)); //n*n행렬이 0으로채워진 c 행렬이 생성된다.
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
for(int k=0; k<n; k++)
c[i][j] += a[i][k] * b[k][j];
return c;
}
O(너비^3)에 곱연산 수행한다.